EntityFramework Code首先具有多对多关系

时间:2015-01-06 22:57:35

标签: entity-framework entity-framework-6

我尝试基于具有3个表的现有数据库创建代码优先模型:

Template
-------------
IdEnvironment
IdTemplate

Context
-------------
IdEnvironment
IdContext

ContextTemplate
-------------
IdEnvironment
IdTemplate
IdContext

我使用以下代码构建模型:

modelBuilder.Entity<Template>()
            .HasMany<Context>(e => e.Contexts)
            .WithMany(e => e.Templates)
            .Map(cs =>
                {
                    cs.MapLeftKey(new string[] { "IdEnvironment", "IdContext" });
                    cs.MapRightKey(new string[] { "IdEnvironment", "IdTemplate" });
                    cs.ToTable("ContextTemplate");
                });

当我尝试创建迁移时,出现以下错误:

One or more validation errors were detected during model generation:
IdEnvironment: Name: Each property name in a type must be unique. Property name 'IdEnvironment' is already defined.
TemplateContext: EntityType: EntitySet 'TemplateContext' is based on type 'TemplateContext' that has no keys defined.

有没有办法在不添加IdEnvironment2列的情况下修复模型?

0 个答案:

没有答案