code-first

Naming conventions in generated many-to-many table using EF4 CTP4 code first approach

Given the following POCO classes: public class Certification { public int Id { get; set; } public virtual ICollection<Employee> CertifiedEmployees { get; set; } } public class Employee { public int Id { get; set; } public virtual ICollection<Certification> Certifications { get; set; } } Creating the database model usi...

In EF4 Code first Collection are not lazy loading?

Using Microsoft Visual C# 2010 Express, Entity Framework Feature CTP4. I tried EF4 with code first with something small based on Scott Gu's blog. But it seems that collections are not initialized when retrieving an entity. I get a null reference exception when adding a product to a category. In all the examples I've seen, the collection...

EF4 Code only TPH change type of object

I am trying to figure out something with EF4 Code Only. If i use TPH and i wanted to change a saved Person to Instructor or vice versa, how would i accomplish this. My POCO classes: public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } publi...

Entity Framework 4 Code First - Virtual properties not updating when changed to null

So I have a model in my domain similar to this: public class Product { public virtual Tag Methodology { get; set; } } Then in an webform project I update it like so: if (!string.IsNullOrWhiteSpace(ddlMethodology.SelectedValue)) product.Methodology = TagRepo.GetTagById(int.Parse(ddlMethodology.SelectedValue)); else product...

Entity Framework 4 CTP 4 Code First: how to work with unconventional primary and foreign key names

Is there a way in Entity Framework 4 (using CTP4 and Code First if that matters) to change the conventions used to automatically identify primary and foreign keys? I'm trying to use EF with a legacy database that uses a "pk/fk" prefix rather than an "id" suffix to mark keys. Also, it's not uncommon to have multiple foreign keys to an A...

Entity Framework CTP4 Code First: Mapping protected properties

I would like to use a lazy-loading collection on a model, but I want Add/Remove functionality to be done through separate methods. So something like this: class Model { protected virtual ICollection<Something> _somethings { get; set; } public IEnumerable<Something> Somethings { get { return _somethings; } } public vo...

1 to 1 Object Relations in EF4 Code First

I have a parent object book, and a property of that object is publisher. Everytime I ad a book, it is adding a new publisher, even if the publisher already exists. Can someone tell me how to add the book and instead of adding the publisher again, just reference an existing one? The code i am using is below... Thanks in advance! publ...

Entity Framework 4 - Code First and Stored Procedures

Hi, I want to execute stored procedure with one parameter that returns table using EF4 "Code First". I am ok with some DTO just for this purpose, it doesn't have to return entities. I have tried to: a) create edmx file with function import and add it to my ObjectContext like this: protected override void OnModelCreating(ModelBuilder m...

The model backing the <Database> context has changed since the database was created.

The error message :- "The model backing the 'AddressBook' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, a...

Entity Framework - CTP4 - Code First - How to turn off the automatic pluralization?

My entity name is "Contact" and my table name is "Contact". However, the default pluralization support is making EF4 to look for a table named "Contacts". Anybody has any idea on how to turn off the pluralization support? This post has got some details on pluralization support. But still does not give me an answer. I see the following...

Entity Framework 4 Code-First many to many insert

I'm using code-first pattern for database layer. I have two POCO classes: public class Order { [Key] public int OrderId { get; set; } public virtual ICollection<Item> Items { get; set; } // other fields } and public class Item { [Key] public int ItemId { get; set; } public virtual ICollection<Order> Order...

EF4 CodeFirst CTP4 - Insert with existing association

If I have an entity that has an association (e.g. Book.Publisher), how do I save a new Book and associate it with an existing Publisher? BTW I don't want to expose FK associations (i.e. PublisherId) in my model. I had been using something like this: var book = new Book { Title="whatever", Publisher = new Publisher { Id = 42 } }; cont...

RIA with Code First EF - Validating a new entity

My problem is that when I add a new entity with a required field, the entity doesn't show the validation error in the UI. I'm using using EF CTP4 Code First. My setup : I have an entity. public class Category { [Key] public int Id { get; set; } [Required] public string Name { get; set; } public string ImageUrl ...

Whats better design/practice: Nullable property or 1 value property and 1 bool "has" property?

I'm working on an ASP.NET MVC app, designing the domain models, using (testing) the new EF Code First feature. I have an Activity entity that may or may not have a Deadline, what is the best way to approach it? 1 property: public DateTime? Deadline {get; set;} and check vs null before using or 2 properties: public DateTime Deadl...

Entity Framework Code First - Eager Loading not working as expected?

I have the following Entity Framework POCO classes: public class Customer { public int Id {get;set;} public string Name {get;set;} public virtual ICollection<Order> Orders {get;set;} } public class Order { public int Id {get;set;} public int CustomerId {get;set;} public int OrderTypeId {get;set;} public v...

Entity Framework - Navigating and Including properties through collections

I've just had a massive blonde moment*, but it's highlighted an annoyance I have with Entity Framework. I have disabled lazy loading so I'm forcing myself to actually think about what data I require in order to keep the application as fast as possible. So in order to return data within a query, I need to make use of the Include method:...

Mapping with entity framework "code first"

Hi, I'm trying to map my entities using Entity Framework "code first", but I have a problem with mapping a complex type. Here my simplified exampled: Domain object looks like: public class Customer { public Address DeliveryAddress {get; set;} } public class Address { public string StreetName {get; set;} public string Stre...

Code First adding to collections? How to use Code First with repositories? Advice me please

EDIT: this happen only on larger scale projects with repositories. Is there anybody using EF4 with code first approach and using repositories? please advice me Hi. Im currently working with EF4 Code First Classes. In my test project I got two classes, Author and Book (author got books). What I'm trying to do is that I HAve a AddBook in ...

How to design database with user editable/versionable content using Code First?

I an developing a page where users will be able to add and modify existing content, its not a wiki per sé but sort of, like SO's editing abilities. I am working with EF4 and the new Code First approach in the latest CTP, so what would be the best class design for this? my current guess is something like this: public class VersionableT...

Chaining MapHierarchy() to add more Cases in EF code first

I'm hoping there will be a crazy linq answer to this, it feels like there should be Current code: MapHierarchy().Case<Folder>(f => new { FolderId = f.Id, f.Name, Type = 1 }) .Case<RootFolder>(f => new { f.RootName, ...