【发布时间】:2016-02-05 16:14:03
【问题描述】:
我正在做一个项目,我想在代码优先方法中将一些属性设置为可选。在我的类文件中,我已将[Required] 添加到我将在SQL 中设置为不为空的属性中,并且没有[Required] 的属性应设置为在SQL 中允许为空,但对于其中包含ID 的某些属性,它设置为在 SQL 中不为空。下面是我的示例类,其中包含在数据库中生成表时应使用的属性。在 MSSQL 中创建新数据库时,我想将 EnvironmentLastDeployedId 属性设置为可选。
public class Version
{
public int Id { get; set; }
[Required]
public string PackageName { get; set; }
public string VersionName { get; set; }
public string OriginalPackageName { get; set; }
[Required]
public string CommitID { get; set; }
public string CommitMessage { get; set; }
public int ApplicationId { get; set; }
public Application Application { get; set; }
public string CreatedBy { get; set; }
public int EnvironmentLastDeployedId { get; set; }
public Environment EnvironmentLastDeployed { get; set; }
public int StatusId { get; set; }
public Status Status { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime ModifiedOn { get; set; }
}
在我的 Context 类文件中,我尝试使用 Fluent API,但我只得到 IsRequired 方法,但没有得到 IsOptional 或允许我将表列设置为允许在数据库中为空的东西。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Version>()
.Property(p => p.EnvironmentLastDeployedId).IsRequired();
}
【问题讨论】:
标签: asp.net entity-framework entity-framework-6 asp.net-core-mvc