English 中文(简体)
Problem mapping one to many relationship with EF CTP4
原标题:

I am trying to define a one to many relationship between Category and Project (a Category can have one or many Projects, a Project can have one or no Category)

public class Project : Entity {

    public virtual string Title { get; set; }

    public virtual Guid? CategoryId { get; set; }
    public virtual Category Category { get; set; }
}

public class Category : Entity {       
    public virtual string Name { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

I have defined the following mappings:

            modelBuilder.Entity<Project>()
            .MapSingleType(p => new {
                ProjectId = p.Id,
                p.CategoryId,
                p.Title,
                p.Slug,
                p.ShortDescription,
                p.Description,
                p.CreatedOn,
                p.UpdatedOn
            })
            .ToTable("Projects");

        modelBuilder.Entity<Category>()
            .MapSingleType(c => new {
                CategoryId = c.Id,
                c.Name,
                c.CreatedOn,
                c.UpdatedOn
            })
            .ToTable("Categories");

        // relationships
        modelBuilder.Entity<Project>()
            .HasOptional<Category>(p => p.Category)
            .WithMany()
            .HasConstraint((p, c) => p.CategoryId == c.Id);

Now although this appears to be working fine, EF is still generating a Categories_Products table (used for many to many associations).

I ve disabled the default database initializer yet this table is still being generated. What am I doing wrong?

Thanks Ben

最佳回答

I removed the project and category mapping code and let EF use default conventions to create the database. This created the relationship I expected (one to many between category and project).

I would add that the only reason that I was explicitly defining the mapping was because EF does not appear to handle base classes very well. I had a base class "Entity" with a single property "Id" that all my entities inherited from. This caused so many problems with CTP4 that I just swapped it with an interface IEntity. This still gave me the constraints I needed when working with generic repository classes.

Hopefully entity base classes will be better supported in the RTM

问题回答

暂无回答




相关问题
named connection not found (Entity framework problem)

I m building multi-project Application where some UserControl, the user control has a Entitymodel object (myDBContainer db = new myDBContainer()), when i drop my user control on my a form i got the ...

EF4 POCO - Updating a navigation property

I have a Recommendation object and a FeedbackLevel object and the FeedbackLevel object is a navigation property inside a Recommendation object Recommendation int EntityKey; FeedbackLevel Level; ...

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven t found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the ...

Using sql date data type and EF4

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn t appear to be a Date data type)...

RIA services, EF and stored procs

I have this problem for 2 months now How can I use stored procedures whith RIA. I was using LinqToSql and everithing works fine. I ve made an class in designer and mapped it to a SP. Now in EF I ...

Mocking Entity Context in EF4

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF. var context = MockRepository.GenerateMock<SomeDBEntities>(); IObjectSet<TxMode> ...

热门标签