Visual Studio update SQL table schema after changing Visual Basic model file - sql-server

Let's say I have a file Orders.vb, with this content:
Imports System.Data.Entity
Imports DbContext
Public Class Order
Public Property id As Guid
Public Property storeNumber As Integer
End Class
Public Class OrderDBContext
Inherits DbContext
Public Property Orders() As DbSet(Of Order)
End Class
The table was automatically created in SQL Server.
Then I change it to this:
...
Public Property storeNumber As Integer?
...
making the storeNumber field nullable.
How do I tell Visual Studio to update the SQL Server table based on the new model update?

Related

In class design are nested classes always better?

The following item will be maintained in a Microsoft SQL Server database and developed using EF code first:
Is it always better to define the classes nested, meaning as below (abbreviated definitions to keep it simple), letting the database do the work of maintaining the relationships:
Public Class Assembly
Public Property assemblyID As Integer
Public Property parts As New List(Of Part)
End Class
Public Class Part
Public Property partID As Integer
Public Property subitems As New List(Of Subitem)
End Class
Public Class Subitem
Public Property subitemID As Integer
Public Property components As New List(Of Component)
End Class
Public Class Component
Public Property componentID As Integer
Public Property elements As New List(Of Element)
End Class
Public Class Element
Public Property elementID As Integer
Public Property Name As New List(Of String)
End Class
Or is there ever ANY reason to keep the classes separate and do the manual work of maintaining the relationship between the records in each class, meaning as below:
Public Class Assembly
Public Property assemblyID As Integer
Public Property parts As New List(Of Integer) 'which would be partIDs
End Class
Public Class Part
Public Property partID As Integer
Public Property subitems As New List(Of Integer) 'which would be subitemIDs
End Class
Public Class Subitem
Public Property subitemID As Integer
Public Property components As New List(Of Integer) 'which would be componentIDs
End Class
Public Class Component
Public Property componentID As Integer
Public Property elements As New List(Of Integer) 'which would be elementIDs
End Class
Public Class Element
Public Property elementID As Integer
Public Property Name As New List(Of String)
End Class
I have always assumed we should design classes that are nested to follow the structure of the actual data. But since this is a deeper nesting I thought I would ask in case there are other design approaches I should consider.
In this particular case, all of the records are unique. I.E. even though I made this look like it's an assembled part, I was just trying to name each level distinctly. But in this case, any one of these layer items will never be used in a different assembly.
If all your structures have the same depth then your model may be the best. Else consider hierarchical structure. Something like this.
Public Class PartModel
Public Property Id as Integer
Public Property Name As String
Public Property ParentId As Integer? ''Nullable(Of Integer)
<ForeignKey("ParentId")>
Public Property SubParts As List(Of PartModel) ''Note no **New** here. New will be in a Controller
End Class

Showing Comma separated values from database in a textarea?

I have really searched and searched and not found an answer. Given that one article can have multiple tags, I want to show the tags as comma separated values so that it looks like Egypt, Sinai, Muslim Brotherhoodin a textarea when the article is edited. I'll post whatever code is needed if that will help in the answer.
My model is:
Partial Public Class be_Posts
<Key>
Public Property PostRowID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
<StringLength(255)>
Public Property Title As String
Public Property Description As String
<AllowHtml> Public Property PostContent As String
Public Property DateCreated As Date?
Public Property DateModified As Date?
<StringLength(50)>
Public Property Author As String
Public Property IsPublished As Boolean?
Public Property IsCommentEnabled As Boolean?
Public Property Raters As Integer?
Public Property Rating As Single?
<StringLength(255)>
Public Property Slug As String
Public Property IsDeleted As Boolean
Public Overridable Property be_PostTag As ICollection(Of be_PostTag)
Public Overridable Property be_Categories As ICollection(Of be_Categories)
End Class
And the be_PostTag model
Partial Public Class be_PostTag
<Key>
Public Property PostTagID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
<StringLength(50)>
Public Property Tag As String
Public Property be_Posts As ICollection(Of be_Posts)
End Class
I am working with an existing database and data and the models were generated from Code First From Database.
I realize I will need an EditorTemplate but beyond that I don't know what to do. How would I bind multiple values to a single textarea?
An image to show what i am talking about:
Solved:
Tossed this into an editor template:
#modeltype IEnumerable(Of BetterBlog.Core.Entities.be_PostTag)
#code
Dim sb As New StringBuilder
For Each x In Model
Dim tags = x.Tag & IIf(x.Equals(Model.Last), "", ", ")
sb.Append(tags)
Next
#Html.TextArea("PostTags", sb.ToString, 10, 50, nothing)
End Code

CollectionViewSource as Class Property Type

I have a Class in my main WPF application which has a Property defined in the class as follows:
Public Class AppExample
Public PropertyName As CollectionViewSource
The project solution also inherits a Class Library (separate project but included in the solution and using the Inherits statement) - in the Class Library I want to do the same thing but I get an error.
Public Class ClassLibraryExample
Public PropertyName as CollectionViewSource
this results in:
Type 'CollectionViewSource' is not defined
How do I fix this?
Add the refernce of PresentationFramework.dll to your class library. It has namespace System.Windows.Data which contains CollectionViewSource
When you are using CollectionViewSource you have to use Data namespace(System.Windows.Data).

LINQ to EF (Entify Framework) does not create partial methods "OnColumnChanged"

I have created a LINQ to EF model (to be used as the model in an mvvm wpf application) but the classes for each table do not contain on[columnname]changed or on[columnname]changing methods. I thought these were auto generated by the framework. I wanted to add to some of my own public partial methods in order to create some data validation as shown in the following page: http://blogs.msdn.com/b/bethmassi/archive/2009/07/07/implementing-validation-in-wpf-on-entity-framework-entities.aspx
The following class is an example of the auto generated code from EF.
Imports System
Imports System.Collections.Generic
Partial Public Class client
Public Property idClient As Integer
Public Property chrFirst As String
Public Property chrLast As String
Public Property chrCompany As String
Public Property chrEmail As String
Public Property chrPhone1 As String
Public Property chrPhone1Ext As String
Public Property chrPhone2 As String
Public Property chrPhone2Ext As String
Public Property chrFax As String
Public Property chrSuite As String
Public Property chrAddess As String
Public Property chrCity As String
Public Property chrProvince As String
Public Property chrCountry As String
Public Property chrPostal As String
Public Property dtCreated As Nullable(Of Date)
Public Property dtUpdated As Nullable(Of Date)
Public Property FTC_Type As Nullable(Of Byte)
Public Overridable Property invoices As ICollection(Of invoice) = New HashSet(Of invoice)
Public Overridable Property jobs As ICollection(Of job) = New HashSet(Of job)
End Class
When I add a partial public class of the same name I can see all the declarations of the original class generated by the EF in the declarations dropdown in the upper right hand corner fo the window in visual studio.
1- Does LINQ to EF work this way?
2- I am using vb.net 4 and visual studio 2012, is there another way of doing this?
Thanks in advance
which template are you using to generate the files?
The Self Tracking Entities are best-suited for use in a client/server WPF application, because they implement INotifyPropertyChanged and allow for disconnected (n-tier) change tracking.

How to convert a DbSet to an observableCollection in WPF?

I have a database which is created using EF4.1 code first. The data context is as follow:
public DbSet<User> Users { get; set; }
public DbSet<Address> Addresses { get; set; }
I want to show address for a user by binding the users.Address to a datagrid in a ViewModel scenario. The problem is that I cannot convert Users.Address into a list that I can bind a collection into it. Any suggestion?
Here are some links on how to use the Local property of DbSet<> for data binding.
Using DbContext in EF 4.1 Part 7: Local Data
EF Feature CTP5: Code First Model with Master-Detail WPF Application

Resources