gpt4 book ai didi

entity-framework - Entity Framework 代码优先属性结合流畅的api配置

转载 作者:行者123 更新时间:2023-12-04 19:04:03 24 4
gpt4 key购买 nike

我可以将代码优先属性与 fluent-API 配置结合用于 Entity Framework 中的实体吗?

谢谢你。

最佳答案

是的你可以。我通常更喜欢定义一些约束(例如,通过使用 [Required] 来创建所需的属性或通过使用 StringhLength(1, 10) 来定义字符串属性的长度):

  [Required]
[StringLentgh(1,10)]
public string BookName {get;set;}

另一方面,我一般使用 fluent api 来定义关系(例如,1 对多关系)
  dbContext.Entity<Book>()
.HasRequired(b => b.Author)
.WithMany(a => a.Books)
.HasForeignKey(b => b.AuthorId)

但是,您可能更喜欢使用 fluent API 来在模型中实现约束。也就是说,您只能使用 fluent API 来做任何事情。但是,数据注释并不是那么全面。检查这些以获取更多信息:

https://stackoverflow.com/a/5356222/1845408

http://www.codeproject.com/Articles/476966/FluentplusAPIplusvsplusDataplusAnnotations-plusWor

http://www.codeproject.com/Articles/368164/EF-Data-Annotations-and-Code-Fluent

关于entity-framework - Entity Framework 代码优先属性结合流畅的api配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29668136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com