简体   繁体   English

EF Core 数据库优先 HasNoKey

[英]EF Core Database First HasNoKey

I have a database table created 20 years ago in my company, it is only used for journaling, with NO PRIMARY KEY or INDEX.我有一个 20 年前在我公司创建的数据库表,它仅用于日记,没有主键或索引。 I can't alter that table.我不能改变那张桌子。

I understood that we need to call HasNoKey() in code first approach.我知道我们需要在代码优先方法中调用HasNoKey()

But how can I apply HasNoKey in this case?但是在这种情况下如何应用 HasNoKey 呢? Since it is a Database First approach.因为它是数据库优先的方法。

In your DbContext class' OnModelCreating method, you do something like this:在您的DbContext类的OnModelCreating方法中,您可以执行以下操作:

modelBuilder
    .Entity<MyEntity>(builder =>
    {
        builder.HasNoKey();
        builder.ToTable("MY_ENTITY");
    });

Read more about it here:在此处阅读更多相关信息:
https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=fluent-api https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=fluent-api

Use [Keyless] Attribute of model class.使用 model class 的[Keyless]属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM