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 Code First One to One Constraint Violation

Given the following simplified model: public class Account { public Account() { Id = Guid.NewGuid(); ContactCard = new ContactCard(); } //[ForeignKey("ContactCard")] public Guid Id { get; set; } public string…
daveywc
  • 3,136
  • 7
  • 45
  • 58
3
votes
1 answer

EF4 code first many to many relationship to external entity

Problem I've got two classes: OfficeProfile: exists within my EF model BusinessUnit: comes from an external source, e.g. a web service There is a many to many relationship between office profiles and business units, which I'd like to represent in…
3
votes
1 answer

How to Count items in nested collection / codefirst EntityFramework

I've got CodeFirst collection defined as defined below. For any given EmailOwnerId, I want to count the number of EmailDetailAttachments records exist without actually downloading all the images themselves. I know I can do something like var…
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188
3
votes
1 answer

EF Code first and initialize multiple contexts

I'm trying out Code First. I've two seperate contexts using the same database. My problem is how to use the databaseinitializers. If I use a initializer that drops the database, it only works for the first context. Then I initialize the secont…
Erik Z
  • 4,660
  • 6
  • 47
  • 74
3
votes
1 answer

DB updates with Automapper / EF CodeFirst

I'm using Automapper in my MVC/EF Code First project. While mapping a ViewModel into View, I am using a customer converter class inherited from TypeConverter. I am setting up the mapping using the below code: Mapper.CreateMap
Balaram
  • 43
  • 3
3
votes
2 answers

Create code first poco classes from database

I know that I can create the POCO files from .edmx, but this only give's you a part of the code, because if you are going to use code first approach you need to provide more info to the POCO clases for example the key and foreign key by annotations…
Jorge
  • 17,896
  • 19
  • 80
  • 126
3
votes
2 answers

EF Code First: Many-to-Many + Additional Property

I have the following tables: Country, Language and Country2Language Country2Language is the linking table that creates the M:M relationship which is defined using Fluent Mapping: mb.Entity() .HasMany(e =>…
Mike
  • 1,992
  • 4
  • 31
  • 42
3
votes
1 answer

Foreign Keys in Entity Framework - Cycles or multiple cascade paths error

Using EF Code First i have the following, for example: public class Blog { public int BlogID { get; set; } public string Content { get; set; } public virtual User User { get; set; } public virtual ICollection BlogMeta {…
Michael Willmott
  • 529
  • 1
  • 8
  • 21
3
votes
1 answer

EntityFramework code first and circular one-to-many reference

please help me with the following problem. i have a class, like public class TopicItem { public TopicItem() { _children = new List(); } public int Id { get; set; } public string Title { get; set; } public…
3
votes
2 answers

Differences in EF Code FIrst and Database first model

I am new in EF and trying to understand the differences in EF Code First and Database first model. What should be the best choice for starting a new MVC3 project?
user1018805
3
votes
2 answers

entity framework 4.1 code first and auto mapper issue

Consider this simple Model and ViewModel scenario: public class SomeModel { public virtual Company company {get; set;} public string name {get; set;} public string address {get; set;} //some other few tens of properties } public…
Jaggu
  • 6,298
  • 16
  • 58
  • 96
3
votes
2 answers

Entity Framework Null Reference When Adding New Record

I currently have a project that uses Entity Framework 4.1 for logging to a database so we can track the web application which is deployed on more than one web server. I built it using Scott Gu's Code First solution. So, I have code like…
Josh
  • 16,286
  • 25
  • 113
  • 158
3
votes
1 answer

EntityType 'Uri' has no key defined

I have a problem with EF 4.1 Here are my entities and context: public class PasmISOContext : DbContext { public DbSet Avatars { get; set; } public DbSet Users { get; set; } } namespace PasmISO.Domain { public class…
user278618
  • 19,306
  • 42
  • 126
  • 196
3
votes
1 answer

Adding a property for the count of associated entities in Entity Framework Code First

I'm using Code First to write my data layer, then transmitting to a Silverlight front end using RIA services. Since I have to serialize everything, I would like to get some additional information on each entity before sending it across the wire (to…
Eric Andres
  • 3,417
  • 2
  • 24
  • 40
3
votes
4 answers

EF Code First - {"CREATE DATABASE permission denied in database 'master'."}

I just want to quickly spin up the default database in my development environment. How is the easiest way to get around this problem?
user880954
  • 7,317
  • 6
  • 22
  • 20