Questions tagged [ef-code-first]

EF Code-First is a way of using Microsoft's Entity Framework with POCO classes, as opposed to model-first or DB-first.

Entity Framework Code-First is a methodology for providing mapping between .NET CLR classes and database structure. Classes and properties may be marked up with attribute decorators (for example [Table("MyTable")] or [Column("CreatedDate")]) or the description for mapping the classes and their properties may be made through the FluentAPI method calls overriding the model creation.

Code-first also operates by convention in that with no markup or FluentAPI coding, it will attempt to connect to a default SQLServer instance to a database named for the project that contains the DBContext class (for example if creating a FoodPantryDAL project where your context will be created, it will attempt to connect to ./SQLExpress/FoodPantryDAL). If the database is not there (and the SQLServer instance is) it will generate the database according to the classes and properties that are currently defined. Tables will be named after the classes they represent, properties also. Properties named ID or [ClassName]ID will be created as a primary key. Classes which reference other classes will be given foreign key relations to those classes, etc.

Code-First may be used not only to create a new database but can all create a mapping to an existing database structure.

8514 questions
3
votes
2 answers

Migrating from Entity Framework Code First from Development to Production

I have been working on a side project for the last few weeks, and built the system with EntityFramework Code first. This was very handy during development, as any changes i needed to make to the code were reflected in the DB nice and easily. But now…
TiernanO
  • 1,597
  • 14
  • 31
3
votes
4 answers

ASP.NET MVC3 - "Object reference not set to an instance of an object" error

I'm relatively new to .NET and MVC3. I'm having some trouble with the above error message when trying to add an instance of an object. Below is my code: KeyDate class public class KeyDate { [Key] public int KeyID { get; set; } …
Gareth Lewis
  • 751
  • 2
  • 15
  • 36
3
votes
1 answer

LinqPad + EF 4.1 + SQL Server CE

I've been toying with the idea for a couple of days, but I can't find any definitive evidence to suggest this will or will not work. What I want to do is use LinqPad queries to create a EF 4.1 code-first database with a SQL Server CE backing in the…
llaughlin
  • 511
  • 7
  • 15
3
votes
1 answer

Class Inheritance with .NET EF4.1 + MySQL

I'm trying to implement class inheritance in .NET using Entity Framework 4.1 and MySQL as database, with code-first approach. The model below works with SQL Server, but fails in MySQL with the following error: Schema specified is not valid. Errors:…
tyron
  • 3,715
  • 1
  • 22
  • 36
3
votes
1 answer

T4 Template Get Attributes

I am trying to customise the T4 templates used in MVCScaffolding. I would like to filter out properties which are have the NotMapped attribute. My problem is that the attribute collection seems to be empty. I have tried printing out the attributes…
row1
  • 5,568
  • 3
  • 46
  • 72
3
votes
2 answers

FileNotFound exception while trying to create ASP.NET MVC 4 controller

I'm trying to follow the Contoso University tutorial, only with SQL Server 2008 instead of CE. I can connect to the database engine in SQL Server Management Studio using my PC's name and windows authentication. I haven't created a database with it…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
3
votes
2 answers

MVC3 - how to link a newly created child object to its parent?

I am a complete novice at MVC, and can't seem to get my head around a very basic concept. I have a parent object, that contains a collection of child objects. I want to create a new child object, and link it to the parent object, persisted in the…
Gerry
  • 53
  • 1
  • 8
3
votes
2 answers

ASP.NET MVC 3 add controller throws error "Object reference not set to an instance of object"

I get below error when I try to add controller to my ASP.NET MVC 3 intranet project Object reference not set to an instance of object I am following an article http://sumitmaitra.wordpress.com/2011/11/13/fun-with-signalr/ to develop SignalR…
pramodtech
  • 6,300
  • 18
  • 72
  • 111
3
votes
2 answers

Is my understanding of IsLoaded correct?

Here is some code along with my assumptions based on playing around in LINQPad. Can anyone confirm this is how the lazy loading is working, and perhaps provide any additional insight/links so I can understand how it's working on the back end? Thanks…
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
3
votes
1 answer

Entity Framework Code First update error with unloaded required navigation property

I found out that, if i set a navigation property as required ( using the Required attribute ) and use lazy-loading proxies, when i load the parent entity and do nothing but try to save in the same context, an EntityValidationError occurs which is…
Cagatay Kalan
  • 4,066
  • 1
  • 30
  • 23
3
votes
1 answer

HasOptional counterpart DataAnnotation attribute

There is an DataAnnotation attribute counterpart for the HasOptional method of the code-first fluent API? I want to mark my property with attribute instead of using the fluent API. This is my current model & OnModelCreating code: public class…
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
3
votes
1 answer

Generate DDL Script to create database in Entity Framework

I am creating an application using Entity Framework code-first. How can I create the database using this model? Is it possible to create the DDL scripts from a code-first approach just like the DDL scripts that can be generated from a model first…
Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66
3
votes
0 answers

EF Mapping to prefix all column names within a table

I have a large existing database which has a convention where all the column names are prefixed with the de-pluralised tablename, e.g. Tablename: addresses Columns: addressId addressLine1 addressLine2 Is there any configuration I can…
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
3
votes
1 answer

EF Code First convention to allow ICollection properties to be empty collections rather than null?

I've noticed that, by default, Entity Framework Code First neglects to instantiate ICollection properties unless there is at least one item in the collection. I would much prefer that the collection were guaranteed to always be an empty HashSet…
devuxer
  • 41,681
  • 47
  • 180
  • 292
3
votes
1 answer

How Do I Add Display Name To An Inherited Property With EF Code First?

I have a class Movement inheriting from TimeBlock. TimeBlock is provided for me and I can't change it. TimeBlock provides a DurationDescription property and I want to display it. However I always use LabelFor etc which means I need to have Display…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111