问题
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.
Thanks in advance
Best Regards
回答1:
Use the DatabaseGenerated
attribute with DatabaseGeneratedOption.Computed
as the value
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Foo { get; set; }
回答2:
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?
回答3:
I think for EFCF beginners (like me!), there is no need for creating Database Initializers, this just work:
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.
来源:https://stackoverflow.com/questions/9254154/entity-framework-code-first-how-to-map-sql-servers-computed-columns-into-cf-mo