实体框架代码第一计算属性 [英] Entity Framework Code First Computed Properties

查看:85
本文介绍了实体框架代码第一计算属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC 3 Web应用程序中使用了Entity FrameworkCode First方法。在我的数据库中,我有几个计算列。我需要使用这些列来显示数据(可以正常工作)。



但是,当我在这个表中插入行时,我收到以下错误: / p>


列ChargePointText不能修改
,因为它是
计算列,或者是
UNION运算符


有没有办法在我的课程中将属性标记为只读方式?

  public class Tariff 
{
public int TariffId {get;组; }
public int Seq {get;组; }
public int TariffType {get;组; }
public int TariffValue {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public int ChargePoint {get;组; }
public string ChargePointText {get;组; }
}


解决方案

解。实体框架提供了一个名为DatabaseGenerated的数据注释。使用它像这样:

  [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 
public string ChargePointText {get;组;

获取更多详细信息: http://msdn.microsoft.com/en-us/library/gg193958.aspx


I'm using Entity Framework "Code First" approach in a ASP.NET MVC 3 web application. In my database I have several computed columns. I need to use these columns to display data (which works fine).

However when I come to insert rows into this table, I'm getting the following error:

The column "ChargePointText" cannot be modified because it is either a computed column or is the result of a UNION operator.

Is there a way to mark the property as readonly in my class?

public class Tariff
{
    public int TariffId { get; set; }
    public int Seq { get; set; }
    public int TariffType { get; set; }
    public int TariffValue { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int ChargePoint { get; set; }
    public string ChargePointText { get; set; }
}

解决方案

I've found the solution. Entity Framework provides a data annotation called DatabaseGenerated. Use it like this:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string ChargePointText { get; set; }

Get more details here: http://msdn.microsoft.com/en-us/library/gg193958.aspx

这篇关于实体框架代码第一计算属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆