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
1 answer

Self referencing model in EF Code First 4.1 throws 'Collection was modified' exception

I am trying to model the following using EF 4.1 and cannot get past this exception ("Collection was modified; enumeration operation may not execute"). Models: public class Workflow { public List Stages { get; set; } } public…
Sean Kenny
  • 1,626
  • 13
  • 14
3
votes
2 answers

ComplexType Collection Property in EF 4.1 with Code First

Is it possible to create a property within a POCO that is a collection of ComplexTypes? [ComplexType] public class Attachment { public string FileName { get; set; } public byte[] RawData { get; set; } public string MimeType {…
3
votes
1 answer

EntityFramework Code First: Why does Lazy Loading of Many to Many Reference fail?

My EF 4.1 Code First model should be fairly self-explanitory if you look at the code below and am trying to use DataAnnotations whenever I can. A franchise sells many items. A franchise has many stores that can sell the items. A store may be sold…
3
votes
1 answer

Prevent EF 4.1 from deleting my database diagram

Is there a way to prevent Entity Framework Code-First from deleting my database diagrams when it is re-building my database?
3
votes
1 answer

EF4 code-first - insanely nested UNION ALL and duplicated joins?

I have a class with quite a few includes in its 'default get method', like this: _dbContext.Users .Include(c => c.PublicContact) .Include(c => c.PrivateContact) .Include(c => c.Product) .Include(c => c.Languages) .Include(c =>…
Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
3
votes
2 answers

How do I setup a foreign key relationship in codefirst without using a navigation property?

Say you have an order class with an order status, I want to declare the OrderStatusId inside the OrderStatus class. However, no foreign key relationship is set-up by default. If I use [ForeignKey] attribute on the column it seems to demand a…
jaffa
  • 26,770
  • 50
  • 178
  • 289
3
votes
2 answers

Load collection except another collection using linq

I have this search method: public List AutoSuggestEmployeee(string keyword, long employeeeTypeId, int count) { return context.Employeees.Where( x => x.EmployeeeName.Contains(keyword) && x.EmployeeeTypeId ==…
Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
3
votes
2 answers

Entity Framework "Code First" not seeding or creating databases - no errors

For some reason my DB is not being created and i'm not getting any errors. I'm using "SQL Server" Tring to initialize the DB in the global.asax.ca file: protected void Application_Start(object sender, EventArgs e) { //BREAKPOINT HITS. GOOD …
vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
3
votes
1 answer

Using EF 4.1, can a complex type reference an entity (e.g. in DDD a value object referencing an entity)?

A blog entry I read seems to indicate it's ok for value objects to reference entities in domain driven design, and the follow-up explains how to do this in NHibernate. I would like to do the same thing using Entity Framework 4.1: specifically, have…
3
votes
1 answer

ef code first: get entity table name without dataannotations

Is there a way to get table info defined with DbModelBuilder? something like: entity.GetType().GetTableName() Max EDIT: id like to implement following public static class Helper { public string GetTableName(Type type) { // ?? } } now i'd…
maxlego
  • 4,864
  • 3
  • 31
  • 38
3
votes
2 answers

Value cannot be null; Parameter Name: constructor when using SqlExecuteQuery with an abstract Entity model

We are storing items in our database which are derived from a single abstract Entity named SurveyItem. We are trying to find SurveyItems that contain some query string. I apologize for the wall of code, but I think this is as small as I can make it…
Alessandro Vermeulen
  • 1,321
  • 1
  • 9
  • 28
3
votes
1 answer

Use Runtime Data to filter Rows in HasQueryFilter() in EF Core fds

In my application there is a Enum which name is RecordType and all of tables contains a field named 'TypeId'. When a user Add a new record, I set TypeId based on user's TypeId. in this way I want to load data realate to each user's Type. RecordType…
FullStack
  • 177
  • 1
  • 3
  • 12
3
votes
1 answer

EF Core Many-to-Many Relation Table Naming

Does EF Core provide a way of naming the many-to-many relations mapping to database tables ? In a code-first pattern, I have the following 2 Entities: [Table("Prefix.Users")] public class User { public int ID { get; set; } public…
3
votes
1 answer

Configure TPH Discriminator column in code first EF 4.1 using attributes

I have a .NET code first EF 4.1 set of objects that I am configuring with attributes. I would like to rename the column used as the discriminator for my Type-Per-Hierarchy inheritance and also control the values of the discriminator column. I've…
codemonkey
  • 1,213
  • 2
  • 14
  • 31
3
votes
3 answers

Can I use strongly typed POCOs as related values with EF code first without creating new ones every time?

I have a status field on a class that has an ID and a Name. I'm not using an enum to model it, but rather a class with some static values, like this: public class MailoutStatus : IEntity { public static MailoutStatus Draft = new…
ssmith
  • 8,092
  • 6
  • 52
  • 93
1 2 3
99
100