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

Entity Framework + Multilevel Inheritance + EF Code first

I'm trying to set up a TPC inheritance using Code First. I have a three level heirarchy. Abstract class A, concrete class B inherits from A and a class C inherits from B. Class A properties: ID, CreatedBy and CreatedOn. Class B properties:…
Amit
  • 501
  • 1
  • 9
  • 16
3
votes
1 answer

How to configure DB connection at run time with EF 4.1 Code First

I'm trying to use EF 4.1 with Code First POCO objects against a legacy database. I have many similar databases all with the same schema, and I need to decide which one to connect to at runtime. All the examples I've seen show to put your connection…
codemonkey
  • 1,213
  • 2
  • 14
  • 31
3
votes
1 answer

EF4 Code first - delete all children when deleting parent from db?

I have a class that gets arranged into a hierarchy, such as this: class TestContainer { ... bla bla bla... public Virtual Item Items {get;set;} } Class Item { [Key] public int ID {get;set;} public string PropA {get;set;} public string PropB…
Yablargo
  • 3,520
  • 7
  • 37
  • 58
3
votes
2 answers

Implement WCF Data Service using the Repository Pattern

We are using the repository pattern in our ASP.NET MVC 3 application. This means that, although we use EF 4.1 Code First to access the data in the backend, all MVC controllers do that via a generic repository class rather than directly over the…
3
votes
1 answer

Cannot change the connectionstring with Entity Framework 4.1 code first

I am having trouble changing the connection string used by entity framework code first for my project. I created a MVC3 project and then added two projects, DomainClasses and DataAccess. The DomainClasses project has one class file named Classes.cs…
3
votes
3 answers

Connection string EF 4.1 code first SQL compact in windows form application

I have created a windows form application: A presentation library with several windows forms A class library with a data layer A class library to access a database I'm using EntityFramework 4.1 with Code First Approach and SQL Compact 4.0…
3
votes
3 answers

Load an .edmx into the DbModelBuilder

I'm planning on using the Entity Framework 4.1 in my next project, but I'm having trouble finding a good way to go about it. In short, I want to build a multi-tiered application in which the entities will be travelling through web services, and to…
Leon Bouquiet
  • 4,159
  • 3
  • 25
  • 36
3
votes
2 answers

Cannot rename Discriminator column in Entity Framework 4.1 Code First database

I have a model hierarchy configuration like so: [Table("A")] abstract class A { } class B : A { } // treated as abstract [Table("C")] class C : B { } This results in a TPH for A and B, and a TPT for C. So far this seems to work fine. There are…
3
votes
1 answer

EF 4.1 Code First Database Initialization System.NullReferenceException Loading Data

I am using EF 4.1 Code First and I am having trouble initializing my data the way I think I should be able to. In my code below, you will see that I have a model that starts with Client and it contains a series of related objects. I load each…
Justin
  • 83
  • 1
  • 3
3
votes
1 answer

Updating Entity Before Deleting

How can I update audit information contained in an entity before actually deleting it? I'm working on a class derived from DbContext. I tried by changing the state to Modified, then set the updated info, then calling base.SaveChanges(), then marking…
Luis Aguilar
  • 4,331
  • 6
  • 36
  • 55
3
votes
1 answer

MVC3 Entity Framework many-to-many with additional column

I'm new to asp.net, mvc3 and entity framework. I'm trying to develop a mvc3 programm with entity framework and code-first. So I have two classes with a many-to-many relationship. One class called "User" the other one is "Course". public class…
3
votes
2 answers

EF Core Model Seed Data imposes plurals in the key names

I have Tag and Post classes (many-to-many relation, code-first) class Post { ICollection Tags {get; set;} = new List(); } class Tag { ICollection Posts {get; set;} = new List(); } Tag and Post classes has…
serge
  • 13,940
  • 35
  • 121
  • 205
3
votes
2 answers

EF Code first Eager loading and OrderBy problem

I have two entities called Category and Product with 1:n relation. I want to get a Category with its childs that childs be in order. This is my linq: _db.Categories.Where(c => c.CategoryID == catID) .Include(c => c.Products.OrderBy(p =>…
3
votes
1 answer

EF Code First using non-Primary key to create one-to-many relationship

I have the following two entities: public class User { public int PKID { get; set; } public string Login { get; set; } public string Username { get; set; } public string Password { get; set; } public virtual…
topnotch228
  • 93
  • 1
  • 4
3
votes
2 answers

Attaching cached disconnected entities in Entity Framework 4.1 Code First

We have the following: Order with UserId and User table. Let's say we want to cache all User objects as disconnected entities and then use them in ASP.NET or web services like environment. Because the environmnet is using UnitOfWork and IOC…
b0rg
  • 1,879
  • 12
  • 17
1 2 3
99
100