随笔 - 394  文章 - 0  评论 - 946  阅读 - 143万 

今天在编写DomainModel和DomainMapper,最后放到OnModelCreating中运行的时候,给我抛出了如下错误:

1
2
3
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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。由于我在映射的时候,写的是:

1
this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);

而在下面进行导航属性指定的时候,ExpertType被指定成了HasOptional类型的:

1
2
3
4
this.HasOptional(x => x.ExpertType)
               .WithMany()
               .HasForeignKey(x => x.TypeID)
               .WillCascadeOnDelete(false);

导致二者产生了冲突,抛出了开头的错误。

知道了原因,解决方法就好办了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
       }
   }

需要说明的是,上面代码中的:

1
this.Property(x => x.TypeID).IsRequired().HasMaxLength(36);

 

可以选择注释,也可以选择不注释。

posted on   程序诗人  阅读(6720)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示