【发布时间】:2019-03-13 00:16:56
【问题描述】:
我有两个这样的课程:
[Table("GameLevels", Schema = "ref")]
public class GameLevel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public double PointsMin { get; set; }
public double PointsMax { get; set; }
}
[Table("GameProfiles", Schema = "usr")]
public class UserGameProfile
{
[Key]
[ForeignKey("ApplicationUser")]
public string Id { get; set; }
public int GamesPlayed { get; set; }
public double Points { get; set; }
public int WinCount { get; set; }
public int LossCount { get; set; }
public int DrawCount { get; set; }
public int ForfeitCount { get; set; }
public int GameLevelId { get; set; }
public virtual GameLevel Level { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
实体框架构建它,以便UserGameProfile 有一个指向GameLevel 的外键。我想这是因为 GameLevelId 属性。
有没有什么方法可以在没有外键的情况下生成表格和导航属性?
我试过了:
modelBuilder.Entity<UserGameProfile>().HasOptional<GameLevel>(x => x.Level).WithMany();
但随后数据库无法构建。出现此错误:
在模型生成过程中检测到一个或多个验证错误:
Project.Domain.Data.UserGameProfile_Level: : 多重性 与 Role 中的引用约束冲突 关系中的“UserGameProfile_Level_Target” 'UserGameProfile_Level'。因为所有的属性在 依赖角色是不可为空的,主要角色的多重性 必须是“1”。
基本上我想要的是零或一到零或多的关系。
我如何保持关卡独立,但又可以向个人资料添加关卡?
【问题讨论】:
标签: c# entity-framework entity-framework-6