Friday, 15 March 2013

c# - How to write a DbContext Class. -



c# - How to write a DbContext Class. -

if have database named storeitems , , have table in database titles prices, , models name cost write this?

public class storeitemsdbcontext : dbcontext { public dbset<price> prices { get; set; } }

i worked through mvc tutorial , trying things on own existing databases have access too. tutorial didn't explain how write db context class gave me , stated does.

if you're going utilize existing database, need turn off database initialization on context.

public class storeitemscontext : dbcontext { public storeitemscontext() : base("yourconnectionstringname") { database.setinitializer<storeitemscontext>(null); } // dbset's here publlic dbset<price> prices { get; set; } }

note: classes , property names should camel-cased first letter capitalized.

if table name doesn't match default entity framework convention of entity class name, pluralized. need tell ef explicitly table map info to/from:

[table("prices")] public class cost { ... }

you should fine without particular entity, used purposes of providing example.

c# sql-server database asp.net-mvc-3

No comments:

Post a Comment