Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c# - How to store an object as a string in database using Entity Framework 6 code first

I am using EF6 code first in a Winform application. In one of my POCO classes, I have a property of type object which could finally be a string or an integer or a member of an enumeration. this is the property:

public object key {get; set;}

and key could be string, integer or a member of an Enum. I am using C#.

How can I store this property as a string in Database. Perhaps there should be some data annotations or some API commands, but I could not find any. I appreciate for any help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can it be, that sometimes an object with a string Key will get an int Key?

int productId = 42;
Product fetchedProduct = this.FindProduct(productId);
if (fetchedProduct.Key.GeType() == typeof(string))
{
    // Key is a string type, change it to an int:
    fetchedProduct.Key = 233;
    SaveChanges();
}

Change Key type is ridiculous?

If you think that this would be ridiculous, then apparently a "Product with a string key" is not a "Product with an int key" and will never be. If you want to change the "Product with a string key" into a "Product with an int key", then you get a completely new Product, not the same product with the some different properties.

If you think that this is the case, classes should be like:

class Product
{
    public int Id {get; set;}
    public string Name {get; set;}
    ...
}
class IntProduct : Product
{
    public int Key {set; set;}
}
public StringProduct : Product
{
    public string Key {get; set;}
}

TODO: of course there is a proper reason why the product with the int key differs from the product with the string key: give the class a proper name.

See the inheritance strategies for entity framework on how you would solve this. Where you create separate tables or put all Products in one table, depends on whether you what you will most query:

  • "Give me all Products that ..."
  • "Give me all StringProducts that ..."

What you ask most should be in one table, such that a join is not needed.

It is normal that my objects can change Key type

If you think that your object changes the type of Key, then apparently there is something in your Product that makes you say "This is a product with a string key" or "This is a product with an int key". Apparently the type of the key is a property of the Product. Add a column that describes what is so special about this product that it has an int Key, not a string Key.

For example: maybe you have a Warehouse. All European Products have an int Key, all British product have a string Key and all Chinese Products an enum Key.

enum ProductOriginCountry
{
    European,
    British,
    Chinese,
}

class Product
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Key {get; set;}
    public ProductOriginCountry OriginatingCountry {get; set;}
}

Now if you decide to sell Chinese Hammers, instead of British Hammers, then you might argue that you are not creating a new Product, but that only the Key and OriginatingCountry changes.

Add some methods to convert the Key to what it stands for:

public static int GetEuropeanProductKey(this Product product)
{
    if (product.OriginatingCountry == ProductOriginCountry.European)
        return Int32.Parse(product.Key);
    else
       // TODO: decide what to return if you ask for the European Product Key
       // for a Chinese Product
}

Although this would work, I still think that it would be better to let your product have three types of Keys:

    public int EuropeanKey {get; set;}
    public string BritishKey {get; set;}
    ...

And a property that helps you to decide whether you have a European Key or a British key


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...