Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架核心-Fluent API未配置模型_C#_Entity Framework Core - Fatal编程技术网

C# 实体框架核心-Fluent API未配置模型

C# 实体框架核心-Fluent API未配置模型,c#,entity-framework-core,C#,Entity Framework Core,我一直在尝试让fluentapi工作来配置我的模型。我找不到哪里不对劲。 它似乎没有强制执行任何约束,但我可以在创建模型时看到代码运行。文档说明Fluent API将覆盖任何以前的注释,即优先 当我在模型中使用数据注释时,一切正常。 下面是一个简单的代码示例(在模型-数据注释中)与不工作的代码示例(在上下文-OnModelCreating中) //在模型中 使用制度; 使用System.Collections.Generic; 使用System.ComponentModel.DataAnnota

我一直在尝试让fluentapi工作来配置我的模型。我找不到哪里不对劲。 它似乎没有强制执行任何约束,但我可以在创建模型时看到代码运行。文档说明Fluent API将覆盖任何以前的注释,即优先

当我在模型中使用数据注释时,一切正常。 下面是一个简单的代码示例(在模型-数据注释中)与不工作的代码示例(在上下文-OnModelCreating中)

//在模型中
使用制度;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Text.Json.Serialization;
命名空间ParkingPermitData.Models
{
公共类许可购买
{
public int PermitPurchaseId{get;set;}
[必需]
公共字符串ParkingPermitNumber{get;set;}
公共字符串PurchaserName{get;set;}
}
}
//在上下文中
使用Microsoft.EntityFrameworkCore;
使用ParkingPermitData.Models;
命名空间ParkingPermitData.Data
{
公共类ParkingPermitContext:DbContext
{
公共停车许可上下文(DbContextOptions选项):基本(选项)
{
}
公共DbSet PermitPurchases{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().ToTable(“PermitPurchase”);
modelBuilder.Entity()
.Property(b=>b.ParkingPermitNumber)
.IsRequired();
}
}
}
我在OnModelCreating中尝试了作为一条语句的代码,以及我在示例中发现的许多其他变体。 我试图建立一些比这更复杂的关系和注释,但不知道为什么它们不起作用,然后尝试了这个非常简单的案例。 这是非常令人沮丧的,我将感谢任何帮助,请

需要注意的一点是,我在早期开发中将InMemoryDatabase用于DBContext

//Startup
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ParkingPermitContext>(opt =>
                            opt.UseInMemoryDatabase("ParkingPermitPurchases"));
    services.AddControllers();
}
//启动
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(opt=>
选择使用MemoryDatabase(“ParkingPermitPurchases”);
services.AddControllers();
}
非常感谢,,
Paul

内存数据库不考虑数据库约束,因为它不是关系数据库。特别是

InMemory将允许您在关系数据库中保存违反引用完整性约束的数据


有关支持的内容的详细信息

因此,使用此设置,您仍然可以插入ParkingPermitNumber设置为null的记录?此外,使用fluent APIYes Olegl后,无需使用属性[Required]。我的想法完全正确,这是我的问题。如果我注释掉或删除[Required],我可以插入空值。如果我调试,我可以看到正在执行的代码。谢谢tchrikch。似乎一些非常有用的节省时间的东西花费了我一些时间。
//Startup
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ParkingPermitContext>(opt =>
                            opt.UseInMemoryDatabase("ParkingPermitPurchases"));
    services.AddControllers();
}