从模型定义 Entity Framework Core 6.0 约束
我正在使用实体框架核心通过迁移创建项目的数据库。 我的问题是:有没有办法在模型的定义中预设约束?
例如:
using System.ComponentModel.DataAnnotations;
namespace net_ref.Models
{
public class Player
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; } = string.Empty;
public bool IsActive { get; set; }
public int TeamId { get; set; }
public int PlayerPositionId { get; set; }
public Team? Team { get; set; }
public PlayerPosition? PlayerPosition { get; set; }
}
}
如果我使用该模型并生成其迁移,我会得到类似的结果:
table.ForeignKey(onDelete: ReferentialAction.Cascade)
有没有一种方法可以
[ForeignKey.Constraing.OnDelete.NoAction]
public int PlayerPositionId { get; set; }
获得类似的结果?
table.ForeignKey(onDelete: ReferentialAction.NoAction)
在运行 --dotnet ef migrations add-- 命令后在迁移中
I'm using entity framework core to create a project's database using migrations.
My question is: Is there any way to pre-set constraints in the model's definition?
For example:
using System.ComponentModel.DataAnnotations;
namespace net_ref.Models
{
public class Player
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; } = string.Empty;
public bool IsActive { get; set; }
public int TeamId { get; set; }
public int PlayerPositionId { get; set; }
public Team? Team { get; set; }
public PlayerPosition? PlayerPosition { get; set; }
}
}
If I use that model and generate its migrations I get something like:
table.ForeignKey(onDelete: ReferentialAction.Cascade)
Is there a way I could do something like
[ForeignKey.Constraing.OnDelete.NoAction]
public int PlayerPositionId { get; set; }
to get something like
table.ForeignKey(onDelete: ReferentialAction.NoAction)
in the migrations once I run the --dotnet ef migrations add-- command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论