Is it possible to map computed columns into Code First model? I'd like to define computed columns with SQL Server and then map them as properties inside my CF model.
是否可以将计算列映射到Code First模型?我想用SQL Server定义计算列,然后将它们映射为我的CF模型中的属性。
Thanks in advance
提前致谢
Best Regards
3 个解决方案
#1
3
Use the DatabaseGenerated
attribute with DatabaseGeneratedOption.Computed
as the value
使用DatabaseGenerated属性和DatabaseGeneratedOption.Computed作为值
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Foo { get; set; }
#2
0
Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can in Model & DB first though). EF will execute some operations in SQL for you, instead of in code, but it doesn't sound like that is what you're looking for. Can you elaborate on what you are trying to achieve?
不确定如何在不使用视图的情况下执行计算列,并且不能在EF Code First中使用视图(尽管可以在Model&DB中使用)。 EF将为您执行SQL中的一些操作,而不是代码,但听起来并不像您正在寻找的那样。你能详细说明你想要实现的目标吗?
#3
0
I think for EFCF beginners (like me!), there is no need for creating Database Initializers, this just work:
我认为对于EFCF初学者(像我一样!),不需要创建数据库初始化器,这只是工作:
Model1 model = new Model1();//Model1 : DbContext
model.Database.CreateIfNotExists();
model.Database.ExecuteSqlCommand("alter table Results drop column Total; alter table Results add Total AS (Arabic + English + Math + Science)");
for the complete answer check here.
如需完整答案,请点击此处。
#1
3
Use the DatabaseGenerated
attribute with DatabaseGeneratedOption.Computed
as the value
使用DatabaseGenerated属性和DatabaseGeneratedOption.Computed作为值
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Foo { get; set; }
#2
0
Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can in Model & DB first though). EF will execute some operations in SQL for you, instead of in code, but it doesn't sound like that is what you're looking for. Can you elaborate on what you are trying to achieve?
不确定如何在不使用视图的情况下执行计算列,并且不能在EF Code First中使用视图(尽管可以在Model&DB中使用)。 EF将为您执行SQL中的一些操作,而不是代码,但听起来并不像您正在寻找的那样。你能详细说明你想要实现的目标吗?
#3
0
I think for EFCF beginners (like me!), there is no need for creating Database Initializers, this just work:
我认为对于EFCF初学者(像我一样!),不需要创建数据库初始化器,这只是工作:
Model1 model = new Model1();//Model1 : DbContext
model.Database.CreateIfNotExists();
model.Database.ExecuteSqlCommand("alter table Results drop column Total; alter table Results add Total AS (Arabic + English + Math + Science)");
for the complete answer check here.
如需完整答案,请点击此处。