EF6迁移MVC5身份2.1 - 多重性在角色

时间:2015-05-13 08:27:55

标签: c# sql-server asp.net-mvc entity-framework asp.net-identity

我基本上有一个新的MVC5 / EF6(个人用户身份验证)项目,我正在尝试稍微修改Identity Users表,以包含国家,州和城市的ID。我能够通过EF Migrations成功创建新列,但在尝试添加FK约束时,我遇到了障碍。我将CountryId,StateId和CityId添加到默认的Identity Users SQL表中,所有这些都设置为可以为空的Int值。

所以,我有三个类似的国家,州,市(显示State.cs)类:

public class State
    {
        [Key]
        public int StateId { get; set; }
        public string StateName { get; set; }

        [ForeignKey("Country")]
        public int CountryId { get; set; }

        public virtual Country Country { get; set; }
        public virtual ICollection<City> Cities { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

然后我在IdentityModels.cs ApplicationUser类中有了这个额外的代码块:

public class ApplicationUser : IdentityUser
    {
        [ForeignKey("Country")]
        public int CountryId { get; set; }

        [ForeignKey("State")]
        public int StateId { get; set; }

        [ForeignKey("City")]
        public int CityId { get; set; }

        public virtual Country Country { get; set; }
        public virtual State State { get; set; }
        public virtual City City { get; set; }
// the rest is default....
}

看起来很简单。但是,它在尝试添加迁移时不喜欢我:

PM> Add-Migration AddLocaleForeignKeys
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

ApplicationUser_City_Source: : Multiplicity is not valid in Role 'ApplicationUser_City_Source' in relationship 'ApplicationUser_City'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
ApplicationUser_Country_Source: : Multiplicity is not valid in Role 'ApplicationUser_Country_Source' in relationship 'ApplicationUser_Country'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
ApplicationUser_State_Source: : Multiplicity is not valid in Role 'ApplicationUser_State_Source' in relationship 'ApplicationUser_State'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

   at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
One or more validation errors were detected during model generation:

ApplicationUser_City_Source: : Multiplicity is not valid in Role 'ApplicationUser_City_Source' in relationship 'ApplicationUser_City'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
ApplicationUser_Country_Source: : Multiplicity is not valid in Role 'ApplicationUser_Country_Source' in relationship 'ApplicationUser_Country'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
ApplicationUser_State_Source: : Multiplicity is not valid in Role 'ApplicationUser_State_Source' in relationship 'ApplicationUser_State'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

我已经阅读了其他一些类似的问题,我希望有人可以在我的代码中向我展示(在精神上受到挑战的条款)我做错了什么,因为我没有从术语中得到它立场(或查看他们的代码)。从基本上解剖了这个链接HERE

,我得到了关于做FK的想法

我也愿意采取其他方式,因为我正在考虑使用所有这些信息做一个UserSettings表,然后用UserId或类似的东西做一个FK约束。但是,这种方式对我来说似乎是一个额外的不必要的表格。

我很感激帮助。

2 个答案:

答案 0 :(得分:0)

问题是你正在创建三个一对一的关系,在这种关系中,EF期望两个相关的表共享相同的主键值(一个表的主键也是PK和外键)其他),但我认为这不是你的情况,因为同一个State可能在几个ApplicationUsers中,所以,你需要配置一对多的关系。

public class State
{
    [Key]
    public int StateId { get; set; }

    public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
}

public class ApplicationUser : IdentityUser
{
    //...
    [ForeignKey("State")]
    public int StateId { get; set; }

    public virtual State State { get; set; }
}

CountryCity实体执行相同操作。

答案 1 :(得分:0)

对于我上面的问题,接受的答案在技术上是正确的。但是,我找到了另一个我正在寻找的答案(上面也提到过),这也巩固了我不赌博的原因。

在我的最后一段中,我注意到我愿意采用其他方式...以下msdn博客也描述了我所指的内容(具有由单个FK链接的单独用户设置表):

http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

我意识到我可以这样做,但现在我正在寻找一个无错模型等等(感谢octavioccl),尝试从头开始生成代码肯定会让我的大脑爆炸。我现在纯粹是从仅示例模式中学习。无论如何,希望这个答案可以帮助其他人节省时间并克服这些学习障碍。

现在,我将不得不看看我是否可以融化我的大脑试图撤消所有EF迁移的东西并回到我开始的地方。也许我会重新开始......再次......