【问题标题】:EF 4: System.Data.Entity.Edm.EdmEntityType: Name: Each type name in a schema must be uniqueEF 4:System.Data.Entity.Edm.EdmEntityType:名称:架构中的每个类型名称都必须是唯一的
【发布时间】:2012-11-06 12:28:10
【问题描述】:

我想使用 EF 代码优先方法。

我已经阅读了这篇文章:

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

并创建了我的 BL 课程

public class AppData
{
    public string Id { get; set; }

    public string Url { get; set; }

    public AppData_OptionsDialog OptionsDialog { get; set; }

    public AppData_Compatibility Compatibility { get; set; }

}

public class AppData_Compatibility
{
    public int Id { get; set; }

    public string Platform { get; set; }

    public string MaxVersion { get; set; }
}


public class AppData_OptionsDialog
{

    public int Id { get; set; }

    public string DisplayName { get; set; }

    public string AppDesc { get; set; }

    public string PrivacyPolicyUrl { get; set; }

    public string TermsOfUseUrl { get; set; }

}


public class AppsDataContext : System.Data.Entity.DbContext
{
    public AppsDataContext() : base("MaMDB") { }

    public DbSet<Conduit.Mam.Common.BlData.AppsData.AppData> AppsData { get; set; }

    public DbSet<Conduit.Mam.Common.BlData.AppsData.AppData_Compatibility> AppData_Compatibilities { get; set; }

    public DbSet<Conduit.Mam.Common.BlData.AppsData.AppData_OptionsDialog> AppData_OptionsDialogs { get; set; }       
}

我已经在数据库中创建了对应的表。

我了解 EF 使用约定优于配置。

那么它是否神奇地将类映射到数据库?无需生成 em

我尝试对这些方法进行测试:

    public IList<Conduit.Mam.Common.BlData.AppsData.AppData> GetAll()
    {
        var apps = from app in AppsDataContext.AppsData
               select app;

        return apps.ToList();
    }

但得到以下错误:

在模型生成过程中检测到一个或多个验证错误:

\tSystem.Data.Entity.Edm.EdmEntityType: 名称:每个类型的名称 架构必须是唯一的。类型名称“AppData_OptionsDialog”已经存在 定义。 \tSystem.Data.Entity.Edm.EdmEntityType:名称:每个类型名称 在一个模式中必须是唯一的。类型名称“AppData_Compatibility”是 已经定义了。

我已经看到了这个答案,但它对我没有帮助

Entity Framework error - "The EntityContainer name must be unique"

【问题讨论】:

  • 我认为您需要将 Navigation 属性和/或 AppData 类的属性设置为虚拟。
  • 据此,没有什么是虚拟的:codeguru.com/csharp/article.php/c19233/…
  • 需要指定外键属性和键属性。 @Elad Benda,您是否在发布的链接中看到“公共虚拟类别类别 {get;set;}”?
  • 您的项目中是否碰巧有多个名为“AppData_OptionsDialog”的类? (即使生活在不同的命名空间中)

标签: c# asp.net asp.net-mvc entity-framework visual-studio-2012


【解决方案1】:

我想我知道问题出在哪里,尽管这已经非常老了,但我现在遇到了同样的问题,似乎 EF 不喜欢它:

public class User_Roles {
    public bool Admin { get; set; }
    public bool Moderator { get; set; }
    public virtual User User { get; set; }
}
public class User {
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string Salt { get; set; }
    public string Password { get; set; }
    public virtual User_Roles Roles { get; set; }
}

在这种情况下,要么需要重命名 User_Roles,要么需要重命名 User 中的 Roles 属性,如下所示:

public class URoles {
    public bool Admin { get; set; }
    public bool Moderator { get; set; }
    public virtual User User { get; set; }
}
public class User {
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string Salt { get; set; }
    public string Password { get; set; }
    public virtual URoles Roles { get; set; }
}

或者您可以简单地更改用户中的“角色”属性:

public class User_Roles {
    public bool Admin { get; set; }
    public bool Moderator { get; set; }
    public virtual User User { get; set; }
}
public class User {
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string Salt { get; set; }
    public string Password { get; set; }
    public virtual User_Roles URoles { get; set; }
}

在您的情况下,这发生在:

public AppData_OptionsDialog OptionsDialog { get; set; }
public AppData_Compatibility Compatibility { get; set; }

要么重命名类,要么重命名属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多