Entity Framework - [Keyless] Data Annotation Missing

好久不见. 提交于 2020-05-08 05:30:07

问题


According with the Microsoft Documentation Here, I should have access to the Attribute for [Keyless] so I can define my Model has Keyless, so that in my DBContext I could have something like:

public DbSet<MyKeylessClass> KeylessModel { get; set; }

And use _context.KeylessModel.FromSqlRaw(...), without having the need to add a PK to it. I have the reference to System.ComponentModel.DataAnnotations and all the Attributes except Keyless are there, what am I missing here?


回答1:


You've probably missed the following part of the Note at the very beginning of the linked documentation topic

The [Keyless] Data Annotation became available in EFCore 5.0.

"became"? "in EFCore 5.0"? Come on, there is no EFCore 5.0 at this time.

Anyway, looks like they are proactively documenting future things, so just be aware of that when something is missing or is different than the documented.




回答2:


Well, the official EF Core 3.0 release documentation doesn't say a word about supporting that attribute yet, instead for keyless types they instruct to use HasNoKey() method:

ModelBuilder.Entity<MyKeylessClass>().HasNoKey()

You can read more here



来源:https://stackoverflow.com/questions/61396213/entity-framework-keyless-data-annotation-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!