实体框架5:使用DatabaseGeneratedOption.Computed选项 [英] Entity Framework 5: Using DatabaseGeneratedOption.Computed option

查看:139
本文介绍了实体框架5:使用DatabaseGeneratedOption.Computed选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用[DatabaseGenerated(DatabaseGeneratedOption.Computed)]属性的EF5代码优先项目。此选项将覆盖我的设置。



请考虑以下SQL表:

 创建表Vehicle(
VehicleId int identity(1,1)不为null,
名称varchar(100)不为null默认值('Not Set')

我正在使用SQL默认构造来设置[名称](如果未设置的话)。



在后面的代码中,我定义了一个类似于以下内容的类:

 公共类Vehicle {
...

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
公共字符串ShoulderYN {get;组; }
}

当我在代码中更新实体时,默认值中设置的值会覆盖我的新设置。



在代码中,我有(伪):

  vehicle.Name ='车辆更新名称'; 
_dbContext.Update(车辆);
_dbContext.SaveChanges();

预期结果是Vehicle.Name ='Update Vehicle of Name'。



实际结果是Vehicle.Name ='Not Set'。



在EF5中是否可以说如果Vehicle.Name为空/空,使用数据库中定义的值吗?否则,如果我在代码中设置了该值,我想使用该值。



谢谢。 p>

史蒂夫

解决方案

显然,没有。并不是很聪明:)


您可能已经读过,Computed选项只是告诉EF不要更新您的列,因为您将自己在DB端计算值。然后,EF只会从您的数据库中返回新计算的值(在您的情况下为未设置)。


您的基本三个选项是-根据EF源代码文档:



  • 无-数据库不生成值。

  • 身份-插入行时数据库生成一个值。 / li>
  • 已计算-插入或更新行时,数据库会生成一个值。


https://github.com/aspnet/EntityFramework6/blob/527ae9ed97e67ed src / EntityFramework / DataAnnotations / Schema / DatabaseGeneratedOption.cs


由于您希望自己做更多的自定义逻辑,因此恐怕您必须自己做。我建议您停止依赖数据库默认约束,并以代码优先的方式进行所有操作。这样,您将具有如下代码:

 公共类Vehicle 
{
public Vehicle()
{
this.Name =未设置;
}

//没有 Generated属性的情况下
public string Name {get;组; }
}

这样,当您创建实体时,它会自动以预期的默认值开始。以后可以通过简单地修改Name属性来更改。


希望它会有所帮助!


I have an EF5 code first project that uses the [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute. This option is overriding my settings.

Consider this SQL table:

CREATE TABLE Vehicle (
  VehicleId  int identity(1,1) not null,
  Name varchar(100) not null default ('Not Set')
)

I am using the SQL default construct to set the [Name] is case it is not set.

In code behind, I have a class defined similar to:

public class Vehicle {
   ...

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

When I update the entity in code, the value set in the default overrides my new setting.

In code, I have (pseudo):

vehicle.Name = 'Update Name of Vehicle';
_dbContext.Update(vehicle);
_dbContext.SaveChanges();

The expected result is Vehicle.Name = 'Update Name of Vehicle'.

The actual result is Vehicle.Name = 'Not Set'.

Is there a way in EF5 to say "if Vehicle.Name is null/empty, use the value defined in the database? Otherwise, if I set the value in code, I want to use this value."

Thanks.

Steve

解决方案

Apparently, no there isn't. It's not that smart :)

As you may already read, Computed option just tells the EF not to update your column, because you will compute a value on the DB-side yourself. EF will then just return newly computed value from your database (which in your case is "Not Set").

Your basic three options are - as per EF Source code documentation:

  • None - The database does not generate values.
  • Identity - The database generates a value when a row is inserted.
  • Computed - The database generates a value when a row is inserted or updated.

https://github.com/aspnet/EntityFramework6/blob/527ae18fe23f7649712e9461de0c90ed67c3dca9/src/EntityFramework/DataAnnotations/Schema/DatabaseGeneratedOption.cs

Since you expect a little more custom logic to be done, I'm afraid you would have to do it yourself. I would suggest you stop relying on database default constraint and do everything in code first approach. This way you would have a code like that:

public class Vehicle
{
  public Vehicle()
  {
    this.Name = "Not set";
  }
  
  // Without 'Generated' attribute
  public string Name { get; set; }
}

This way, when your Entity is created, it automatically starts with expected default value. And can be later changed by simply modifying the Name property.

Hope it helps!

这篇关于实体框架5:使用DatabaseGeneratedOption.Computed选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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