MVC出错案例之一:主外键映射失败
今天在编写DomainModel和DomainMapper,最后放到OnModelCreating中运行的时候,给我抛出了如下错误:
One or more validation errors were detected during model generation: TinyFrame.Data.DataContext.t_expert_content_ExpertType: : Multiplicity conflicts with the referential constraint in Role 't_expert_content_ExpertType_Target' in relationship 't_expert_content_ExpertType'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.
排查了一会儿找到了原因所在。
请看我的Model:
public class t_expert_content
{
public string ID { get; set; } //PK
public string TypeID { get; set; } public string Name { get; set; } //问题标题
public string Publisher { get; set; } //发布人
public int? Count { get; set; } //查看次数
public bool IsCheck { get; set; } //是否审核
public DateTime PublishDate { get; set; } //发布时间 public string Content { get; set; }
public int? Order { get; set; } public virtual t_expert_type ExpertType { get; set; }
}
然后是ModelMapper:
public class t_expert_content_mapper : EntityTypeConfiguration<t_expert_content>
{
public t_expert_content_mapper()
{
this.ToTable("t_expert_content"); this.HasKey(x => x.ID);
this.Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(x => x.ID).HasMaxLength(36); this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);
this.Property(x => x.Name).IsRequired().HasMaxLength(500);
this.Property(x => x.Publisher).IsOptional().HasMaxLength(150);
this.Property(x => x.Count).IsOptional();
this.Property(x => x.IsCheck).IsRequired();
this.Property(x => x.PublishDate).IsRequired();
this.Property(x => x.Content).IsOptional().HasColumnType("text");
this.Property(x => x.Order).IsOptional(); this.HasOptional(x => x.ExpertType)
.WithMany()
.HasForeignKey(x => x.TypeID)
.WillCascadeOnDelete(false);
}
}
看上去没啥问题,但是问题就出在外键映射上面。
由于t_expert_content的外键TypeID 是t_expert_type表中的主键ID。由于我在映射的时候,写的是:
this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);
而在下面进行导航属性指定的时候,ExpertType被指定成了HasOptional类型的:
this.HasOptional(x => x.ExpertType)
.WithMany()
.HasForeignKey(x => x.TypeID)
.WillCascadeOnDelete(false);
导致二者产生了冲突,抛出了开头的错误。
知道了原因,解决方法就好办了:
public class t_expert_content_mapper : EntityTypeConfiguration<t_expert_content>
{
public t_expert_content_mapper()
{
this.ToTable("t_expert_content"); this.HasKey(x => x.ID);
this.Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(x => x.ID).HasMaxLength(36); //this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);
this.Property(x => x.Name).IsRequired().HasMaxLength(500);
this.Property(x => x.Publisher).IsOptional().HasMaxLength(150);
this.Property(x => x.Count).IsOptional();
this.Property(x => x.IsCheck).IsRequired();
this.Property(x => x.PublishDate).IsRequired();
this.Property(x => x.Content).IsOptional().HasColumnType("text");
this.Property(x => x.Order).IsOptional(); this.HasRequired(x => x.ExpertType)
.WithMany()
.HasForeignKey(x => x.TypeID)
.WillCascadeOnDelete(false);
}
}
需要说明的是,上面代码中的:
this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);
可以选择注释,也可以选择不注释。
MVC出错案例之一:主外键映射失败的更多相关文章
- netcore2.0 ORM框架中如何配置自定义的主外键加载
环境:netcore2.0 DB :mysql ORM:Ant https://github.com/yuzd/AntData.ORM/tree/netcore2 [给我一个star吧] NUGET: ...
- Hibernate之关联关系映射(一对一主键映射和一对一外键映射)
1:Hibernate的关联关系映射的一对一外键映射: 1.1:第一首先引包,省略 1.2:第二创建实体类: 这里使用用户信息和身份证信息的关系,用户的主键编号既可以做身份证信息的主键又可以做身份证信 ...
- hibernate中基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同
基于主键映射1-1关联关系和基于外键映射1-1关联关系的不同,主要区别是在配置映射文件上会有区别 两个持久化类为Manager和Department 1:基于主键映射1-1关联关系 1)使用其他持久化 ...
- Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射
Hibernate中映射一对一关联(按主键映射和外键映射)和组件映射 Hibernate提供了两 ...
- 经典SQL语句大全_主外键_约束
一.基础(建表.建约束.关系) 约束(Constraint)是Microsoft SQL Server 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件(有关数据完整 ...
- Hibernate(八):基于外键映射的1-1关联关系
背景: 一个部门只有一个一把手,这在程序开发中就会设计数据映射应该设置为一对一关联. 在hibernate代码开发中,实现这个业务有两种方案: 1)基于外键映射的1-1关联: 2)基于主键映射的1-1 ...
- EntityFramework Core 迁移忽略主外键关系
前言 本文来源于一位公众号童鞋私信我的问题,在我若加思索后给出了其中一种方案,在此之前我也思考过这个问题,借此机会我稍微看了下,目前能够想到的也只是本文所述方案. 为何要忽略主外键关系 我们不仅疑惑为 ...
- Android Ormlite 学习笔记2 -- 主外键关系
以上一篇为例子,进行主外键的查询 定义Users.java 和 Role.java Users -- Role 关系为:1对1 即父表关系 Role -- Users 关系为:1对多 即子表关系 下面 ...
- 数据库的SQL语句创建和主外键删除操作
create table UserType ( Id ,), Name nvarchar() not null ) go create table UserInfo ( Id ,), LoginPwd ...
随机推荐
- C语言-05-内存分析
一.进制 1> 内存细节 根据数据类型分配相应大小的内存空间 内存空间由高地址向低地址分配 数据一般按照从高位到低位存储 2> 常用的进制格式 十进制 ① 由0~9十个数字组成 ② 逢10 ...
- android network develop(3)----Xml Parser
Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlp ...
- Effective Java 01 Consider static factory methods instead of constructors
Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Ra ...
- Effective Java 51 Beware the performance of string concatenation
Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic ...
- python基本数据结构-集合-集合运算
- ubuntu下ROS安装时sudo rosdep init和rosdep update的解决方法
问题: 在ubuntu上多次安装matlab选择合适的版本来调用摄像头,终于把系统搞坏了,重装系统后,ROS无法安装,每次安装到sudo rosdep init和rosdep update报错的问题, ...
- dipole antenna simulation by CST
CST偶极子天线仿真,半波振子天线 一.本文使用CST仿真频率为1GHz的偶极子天线,使用2013版本.仿真的步骤为 1.选择一个CST的天线工程模板 2.设置好默认的单位 3.设置背景的材料(空气腔 ...
- 边工作边刷题:70天一遍leetcode: day 71-1
Longest Substring with At Most K Distinct Characters 要点:要搞清楚At Most Two Distinct和Longest Substring W ...
- 边工作边刷题:70天一遍leetcode: day 72
Missing Range 要点:题简单,这类题的特点都是记录上一步的状态,比如这题是end 错误点: 三种情况:一是连续的,即和上一个end差1,而是中间只差1个数,没有'->',最后是大于1 ...
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...