views:

21

answers:

1

We have two entities with identical columns but the entity name is different. Can i create 2 entity using the first entity instance ?

We tried doing .AddObject("Entity2 name",entityOneinstance) but it is failing.

Please suggest whether this is possible or any other approach.

Thanks in advance

A: 

Since the types of entities are different, your add operation will fall for sure.

You will need a mapper or (explicit/implicit) conversion operator between your entity types I think.

To make it clear, for the conversation solution, suppose you have Entity1 and Entity2 and both have properties, Property, Property_1, Property_2 and Property_3. I assume that you have default code generation strategy (not POCO or sth). Then you can add partial Entity2 and Entity1 classes with implicit conversion operatior, for example:

public partial class Entity2
{
    public static implicit operator Entity2(Entity1 entity1)
    {
        return new Entity2()
        {
            Property = entity1.Property,
            Property_1 = entity1.Property_1,
            Property_2 = entity1.Property_2,
            Property_3 = entity1.Property_3
        };
    }
}

So you can now do:

using (var provider = new Model1Container12())
{
    Entity1 entity1 = new Entity1();
    provider.AddObject(provider.Entity2Set.Name, entity1);
    // or
    provider.AddToEntity2Set(entity1);
}

The conversion will be made implicitly as you define in the conversion operator definition.

I don't know if Entity Framework itself has a solution for this situation but conversion seems like a solution for me. Or you can also use AutoMapper kind of thing. I don't have detailed information on that.

Musa Hafalır
Mean that i have to create separate entity instances for each ? either of entity can't be reused to add another entity into the database table ? Kindly clarify.
Aaron
I guess you need to create an instance of type which you want to add to your entity context. I have updated my answer. Hope to be helpful. Please notify if the answer is not clear, or I misunderstand the situation.
Musa Hafalır