实体框架6 - 仅允许非空间基元类型使用默认值。 [英] Entity Framework 6 - Default Values are allowed only for non-spatial primitive types.

查看:113
本文介绍了实体框架6 - 仅允许非空间基元类型使用默认值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我希望你们都很好,这里的某个人可能会对我的问题有所了解。



我有一个数据库表,其中包含以下设置:



Hi Guys,

I hope you are all well and that someone here might be able to shed some light on my issue.

I have a database table with the following setup:

ID [Bigint]<br />
Name [nvarchar(250)]<br />
ISOCode [nvarchar(5)]<br />
MarketType [tinyint]





首先使用EntityFramework 6数据库,我已将此表添加到我的edmx文件中,它会生成一个名为Market的类。



除此之外,我创建了两个类,'SingleMarket'和'CombinedMarket'(你们中的一些人可能会想到da-ja-vu,但这与我关于相同对象的最后一个问题不同。) br />
我已将Market上的Type属性转换为Enum,它有两个选项:





Using EntityFramework 6 Database first, I have added this table to my edmx file and it generates a class called "Market".

Off of this, I have created two classes, 'SingleMarket' and 'CombinedMarket' (Some of you may be thinking da-ja-vu here but this is different to my last question relating to the same objects).
I have converted the Type property on my Market to be an Enum which has two options:

Single = 1,<br />
Combined = 2





这些确定对象是单一市场还是组合市场,因为我我相信你现在收集了。

我的问题是,现在我的edmx失败,因此构建时出现错误

仅允许非空间的默认值原始类型



现在我知道这个错误是由我在市场基类上的MarketType属性引起的,但是我想要MarketType默认为1(单个)。我还想在我的实体上保留MarketType属性。



如果我删除了MarketType属性然后它全部编译,但后来我不能说什么类型是这个市场,或者如果我可以,我不知道如何。



我也有这个错误:



These determine whether an object is a Single or Combined market, as I'm sure you gathered by now.
My issue is that right now my edmx fails so build with the error
"Default Values are allowed only for non-spatial primitive types"

Now I know that this error is caused by my "MarketType" property on the market base class, but I want the MarketType to default to 1 (Single). I also want to keep the "MarketType" property on my entity.

If I delete the MarketType property then it all compiles, but then I can't say "What type is this Market", or if I can I don't know how to.

I also have this error:
"

Problem in mapping fragments starting at lines 290, 298, 304:Column Market.MarketType has no default value and is not nullable. A column value is required to store entity data.<br />
An Entity with Key (PK) will not round-trip when:<br />
  ( PK is in 'Markets' EntitySet AND Entity is type [MRPData.Market])





所以,在简单的说,如何将我的MarketType属性保留在基类上并将其设置为默认值1(单个)?



需要任何问题/说明请问。



提前致谢。



类型属性根据Dave的名称重命名为MarketType建议。[/ Edit]

"

So, in simple, how can I keep my "MarketType" property on the base class and have it set with a default value of 1 (single) ?

Any questions/clarification needed please ask.

Thanks in advance.

Type property renamed to MarketType as per Dave's advice.[/Edit]

推荐答案

第一个问题是你为什么使用Type?这不是一个好名字,因为它是.NET使用的类名,会导致代码混乱。



我认为你的类型应该是MarketType,也可以是您的枚举的名称。

The first question is why are you using "Type"? That is not a good name to use as it is a class name that is used by .NET and is going to lead to confusion in the code.

I think your Type should become MarketType, and could also be the name of your Enum.
public enum MarketType
{
    Single,
    Combined
}



并在您的实体模型中:


and in your entity model:

public class Market
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string IsoCode { get; set; }
    public MarketType MarketType { get; set; }
}


这篇关于实体框架6 - 仅允许非空间基元类型使用默认值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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