【问题标题】:MVC4 duplicated UserProfile tableMVC4 重复的 UserProfile 表
【发布时间】:2013-07-25 13:43:55
【问题描述】:

在我的项目中,我使用 MVC4 和一些使用 Entity 作为 orm 的外部数据库。

我决定使用 MVC 提供的成员资格,所以我只是将默认 ConnectionString 更改为指向我的外部数据库。

然后,当我第一次启动应用程序时,添加了几个表格,到目前为止一切都很好。现在,问题是,当我将新创建的 userProfile 表映射到我的 dataContext 模型中时,我遇到了冲突,因为该表已经存在于 accountModel 中。

帐户模型和我新生成的模型在同一个命名空间中,我不想更改,我该怎么办?

这是 ADO 实体模型使用视图添加表方法生成的类:

public partial class UserProfile
    {
        public UserProfile()
        {
            this.Predictions = new HashSet<Prediction>();
        }

        public int UserId { get; set; }
        public string UserName { get; set; }

        public virtual ICollection<Prediction> Predictions { get; set; }
    }

这里来自会员

[Table("UserProfile")]
    public partial class UserProfile
        {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        }

两者都存在于同一个名称空间中,并且存在冲突。

【问题讨论】:

  • 您能提供更多信息吗?在 global.asax 或名为“InitializeSimpleMembershipAttribute”的类中的过滤器文件夹中写入的连接字符串是什么,看起来像这样 WebSecurity.InitializeDatabaseConnection(DEFAULTCONNECTION, “UserProfile”, “UserId”, “UserName”, autoCreateTables: true) ;
  • 我没有完全理解你,我找到了你提到的这个方法 WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);然后我的 webconfig 中的 DefaultConnection 如下所示:
  • 问题是这个成员已经定义了 userProfile 类,但是假设我在我的主模型中有另一个与 userProfil 相关的表,所以我映射了这两个表:这个表和 userProfile 表,然后我们有一个问题,第一视觉说,这个类有 allrady 定义,所以让它成为部分,但我不想手动更改模型,但即使我更改为部分,然后我们有冗余字段等等......
  • 您是否使用实体框架代码优先?您可以将 dataContext 添加到您的问题中吗?
  • 不,我没有先使用代码,我说的是“地图表”一词,我使用的是设计器

标签: asp.net-mvc entity-framework asp.net-mvc-4


【解决方案1】:

您可以删除自动生成的类并创建一个部分类来扩展 AccountModel UserProfile 类。

创建一个同名同命名空间的部分类:

public partial class UserProfile
{
    public UserProfile()
    {
        this.Predictions = new HashSet<Prediction>();
    }        

    public virtual ICollection<Prediction> Predictions { get; set; }
}

通过这样做,您将Predictions 属性添加到成员UserProfile 类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多