简体   繁体   English

数据库优先方法缺少实体框架导航属性

[英]Entity Framework navigation properties missing in DataBase first approach

I have received database scripts from customers which cannot be changed. 我从客户那里收到了无法更改的数据库脚本。 When I am trying to create entities from those scripts using EntityFramework Database First approach , referential relationship is missing from the classes : 当我尝试使用EntityFramework Database First方法从这些脚本创建实体时,类中缺少引用关系:

CREATE TABLE agency(
    tenant_sktmp            INT             identity(1,1) not null,
    rec_create_tmstptmp     DATETIME        NOT NULL,
    rec_update_tmstptmp     DATETIME        NOT NULL,
    rec_created_bytmp       VARCHAR(50),
    rec_updated_bytmp       VARCHAR(50),
    env_nametmp             VARCHAR(100)    NOT NULL,
    product_app_nametmp     VARCHAR(100)    NOT NULL,
    tenant_idtmp            VARCHAR(50)     NOT NULL,
    tenant_nametmp          VARCHAR(100)    NOT NULL
    PRIMARY KEY (tenant_sktmp)
)


CREATE TABLE usertmp(
    user_sktmp             INT             identity(1,1) not null,
    rec_create_tmstptmp    DATETIME        NOT NULL,
    rec_update_tmstptmp    DATETIME        NOT NULL,
    rec_created_bytmp      VARCHAR(50),
    rec_updated_bytmp      VARCHAR(50),
    user_nametmp           VARCHAR(100)    NOT NULL,
    tenant_idtmp           VARCHAR(50)     NOT NULL,
    env_nametmp            VARCHAR(100)    NOT NULL,
    user_pwd_hashtmp       VARCHAR(255)    NOT NULL,
    user_first_nametmp     VARCHAR(200)    NOT NULL,
    user_last_nametmp      VARCHAR(200)    NOT NULL,
    user_hire_datetmp      DATETIME,
    user_birth_datetmp     DATETIME,
    user_staff_idtmp       VARCHAR(50),
    user_santrax_idtmp     VARCHAR(50),
    user_guidtmp           VARCHAR(128),
    PRIMARY KEY (user_sktmp)
)

CREATE UNIQUE INDEX tmp12 ON agency(tenant_idtmp, env_nametmp)
CREATE INDEX ref64tmp ON agency(env_nametmp)


CREATE UNIQUE INDEX user_key1qqq ON usertmp(user_nametmp, env_nametmp, tenant_idtmp)
CREATE INDEX ref56qq ON usertmp(tenant_idtmp, env_nametmp)
CREATE INDEX ref67qq ON usertmp(env_nametmp)

ALTER TABLE usertmp ADD CONSTRAINT Reftenant6tmp 
    FOREIGN KEY (tenant_idtmp, env_nametmp)
    REFERENCES agency(tenant_idtmp, env_nametmp)

The classes created from EF database first approach is follows : 从EF数据库优先方法创建的类如下:

public partial class usertmp
    {
        public int user_sktmp { get; set; }
        public System.DateTime rec_create_tmstptmp { get; set; }
        public System.DateTime rec_update_tmstptmp { get; set; }
        public string rec_created_bytmp { get; set; }
        public string rec_updated_bytmp { get; set; }
        public string user_nametmp { get; set; }
        public string tenant_idtmp { get; set; }
        public string env_nametmp { get; set; }
        public string user_pwd_hashtmp { get; set; }
        public string user_first_nametmp { get; set; }
        public string user_last_nametmp { get; set; }
        public Nullable<System.DateTime> user_hire_datetmp { get; set; }
        public Nullable<System.DateTime> user_birth_datetmp { get; set; }
        public string user_staff_idtmp { get; set; }
        public string user_santrax_idtmp { get; set; }
        public string user_guidtmp { get; set; }
    }
public partial class agency
    {
        public int tenant_sktmp { get; set; }
        public System.DateTime rec_create_tmstptmp { get; set; }
        public System.DateTime rec_update_tmstptmp { get; set; }
        public string rec_created_bytmp { get; set; }
        public string rec_updated_bytmp { get; set; }
        public string env_nametmp { get; set; }
        public string product_app_nametmp { get; set; }
        public string tenant_idtmp { get; set; }
        public string tenant_nametmp { get; set; }
    }

We can see that relationship is missing between the two classes. 我们可以看到这两个类之间缺少关系。 How can I make sure , relation ship remains as I have foreign keys in usertmp table which references agency table. 我如何确定,关系引用仍然存在,因为我在引用代理表的usertmp表中具有外键。

Sql scripts cannot be changed . Sql脚本无法更改。 Any help would be appreciated. 任何帮助,将不胜感激。

If you do not have control over the schema, and you are using non-key fields for linking, then you can relate the tables via JOIN Statements . 如果您无法控制模式,并且使用非关键字段进行链接,则可以通过JOIN语句关联表。 See below. 见下文。

Also, you can use annotations to map more friendly class properties to the less readable SQL column names(You need to do this in partial classes so it is not wiped out when you regenerate): 另外,您可以使用批注将更友好的类属性映射到可读性较低的SQL列名称(您需要在部分类中执行此操作,以便在重新生成时不会被擦除):

public partial class usertmp
    {
    ...
    [Column("user_first_nametmp")]
    public string FirstName { get; set; }
    [Column("user_last_nametmp")]
    public string LastName { get; set; }
    [Column("user_birth_datetmp")]
    public Nullable<System.DateTime> BirthDate { get; set; }
    ...
    }

So now you could JOIN with: 所以现在您可以加入:

var query =
    from u in usertmp
    join a in agency on u.tenant_idtmp equals a.tenant_idtmp
    select new
    {
        FirstName = u.FirstName,
        LastName = u.LastName,
        BirthDate = u.BirthDate,
        AgencyName = a.env_nametmp // You could map this to a friendly name as well
    };

You could select the results into a ViewModel or an anonymous object as above. 您可以将结果选择到如上所述的ViewModel或匿名对象中。

暂无
暂无

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

相关问题 如何在 Entity Framework 6 Database First 中对导航属性实现乐观并发 - How to implement optimistic concurrency on navigation properties in Entity Framework 6 Database First 带有实体框架 6 的 Postgresql(数据库优先方法) - Postgresql with Entity Framework 6 (database first approach) 迁移 - 实体框架数据库优先方法 - Migrations - Entity Framework Database First Approach 实体框架数据库中的存储过程第一种方法 - Stored procedure in Entity Framework database first approach 实体框架 5(代码优先)导航属性 - Entity Framework 5 (Code First) Navigation Properties 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? 如何在实体框架中获取数据库表的列属性(数据库优先方法) - How can I get column properties of DB table in entity framework (database first approach) 如何通过实体框架代码优先方法定义导航属性 - How to define a navigation property via Entity Framework code first approach 处置上下文后,如何从实体框架(数据库优先)返回包含导航属性的模型? - How do I return a Model from Entity Framework (Database First) that includes the Navigation Properties after the context is disposed? 实体框架6.1:代码优先导航属性没有在数据库中定义的对应键 - Entity Framework 6.1: Code First navigation properties that do not have corresponding keys defined in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM