[英]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.