Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]


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


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])
"

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.

[Edit]Type property renamed to MarketType as per Dave's advice.[/Edit]
Posted
Updated 14-Apr-14 5:32am
v2

1 solution

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.
C#
public enum MarketType
{
    Single,
    Combined
}

and in your entity model:
C#
public class Market
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string IsoCode { get; set; }
    public MarketType MarketType { get; set; }
}
 
Share this answer
 
Comments
Pheonyx 14-Apr-14 11:30am    
Hi Dave,

I made that change but it hasn't made a difference to the issue I'm getting. Even when typing this question I didn't even register that I had called it Type thanks for pointing that out :-)
Dave Kreskowiak 16-Apr-14 12:38pm    
I don't know enough about your model to make any determination. Have you seen this: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900