zoukankan      html  css  js  c++  java
  • EF FluentAPI映射一对多 关系时候报错

    提示很明显,不可为空的外键为空了,但是 问题是,我只是初始化 关系映射而已:见代码

    public ColumnsCategoryMapConfiguration()
    {
    ToTable("ColumnsCategory").HasKey(x => x.Id);
    Property(x => x.Id)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
    .IsRequired()
    .IsConcurrencyToken();

    Property(x => x.CategoryName).HasMaxLength(50).IsRequired();

    Property(x => x.SystemMenuKey).IsOptional();//后加的
    HasOptional(x => x.SystemMenu)
    .WithMany(x => x.ColumnsCategoryCollection)
    .HasForeignKey(x => x.SystemMenuKey)
    .WillCascadeOnDelete(false);

    }

    关于 HasOptional;见截图:

    明明是说在数据库中将是可null类型吗?搞什么灰机,难道被微软骗了?

    后来在 stackoverflow上看到一哥们儿针对以类似问题,这么回答

    Hope this is still on time to help you. I was also having the exact same problem and was troubling with it for almost an hour until I could spot my mistake.

    The problem is that Course.Venue relationship is optional (as declared on the fluent API), but the Id declaration of Course.VenueId is mandatory, so you can either make VenueId optional by changing it to

    public int? VenueId { get; set;}

    or change the relationship to mandatory on the fluent API, and the OnModelCreating should run fine once you changed that.

    于是 加上了红色部分 内容,问题即可消除。

  • 相关阅读:
    Python中的Dictionary
    Python中的list
    Python的string模块化方法
    Python字符串格式化表达式和格式化方法
    Python中的slice操作
    Python中的字符串
    华为笔试——C++进制转换
    华为笔试——C++消重输出
    华为笔试——C++转换字符串问题
    C++数字三角形问题与dp算法
  • 原文地址:https://www.cnblogs.com/Tmc-Blog/p/5077281.html
Copyright © 2011-2022 走看看