Last post Jan 09, 2015 11:44 AM by Mikesdotnetting
Jan 09, 2015 10:48 AM|rokosz|LINK
I'm working through the asp.net4.5 web forms tutorial.
The accompanying e-book for the tutorial ~pg60 gets into the app_data folder/ datalevel of the solution. It seems Entity Framework could be really useful. But there are things happening that I can't suss out.
What I can't find is where, in the creation of the tables/classes (ie products.cs), the foreign keys, primary keys and attributes are defined in the sql-DDL. I did find, just a bit later in the tutorial (~pg75) the class def for the cartitem, which clarifies
only a little: "Entity Framework Code First expects that the primary key for the CartItem table will be either CartItemId or ID. However, the code overrides the default
behavior by using the data annotation [Key] attribute. The Key attribute of the ItemId property specifies that the ItemID property is the primary key."
Why would EFCF expect the PK for the cartitem will be "cartitemID, or ID"? At first I thought, simply because "ID" exists or "[tablename]ID" exists, it will be assumed a PK? But the table name is ProductS (plural) and the column name
is ProductID (singular). There is no mention of foreign keys in the tutorial.
Besides the PK and FK, where is the nulls attr set? and the difference in lengths for Description and ImagePath? they result in MAX, but description is explicitly set to 10000 for the description, and the [default] for imagepath.
It seems EFCF makes assumptions, I presume a human has some control of them?
Thanks to all!
The results after the build/initial run:
Jan 09, 2015 11:40 AM|IbrahimIslam|LINK
If you are referring to ProductID being turned into a Primary Key then it is because of Primary key convention in EF where if it find a property that ends with "ID" then it is generated as PK in database. You can learn about all the conventions
here: http://msdn.microsoft.com/en-us/data/jj679962.aspx and you can configure your entity classes according to your will using
Data Annotations or
Jan 09, 2015 11:44 AM|Mikesdotnetting|LINK
You can find the conventions here: http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions%28v=vs.103%29.aspx.
You have to follow each link for a description of the default behaviour.