【发布时间】:2015-05-13 08:27:55
【问题描述】:
我基本上有一个新的 MVC5/EF6(个人用户身份验证)项目,我正在尝试稍微修改 Identity Users 表以包含国家、州和城市的 Id。我能够通过 EF 迁移成功创建新列,但是在尝试添加 FK 约束时遇到了障碍。我在默认的 Identity Users SQL 表中添加了 CountryId、StateId 和 CityId,所有这些都设置为可为空的 Int 值。
所以,我为 Country、State、City 设置了三个类似的类(显示了 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 约束。但是,这种方式对我来说似乎是一个额外的不必要的表。
感谢您的帮助。
【问题讨论】:
标签: c# sql-server asp.net-mvc entity-framework asp.net-identity