开发者

Entity Framework 4.0 . Entity Creation

We have two entities with identical columns but the entity name is different. Can i 开发者_StackOverflow中文版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


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.


In EF4, ObjectSet was introduced, which is kinda nifty..

My approach would be using repository pattern...

First create an abstract base class..

    public abstract class BaseRepository<T> where T : class
{
    #region Members

    protected IObjectSet<T> _objectSet;

    #endregion

    #region Ctor

    public BaseRepository(ObjectContext context)
    {
        _objectSet = context.CreateObjectSet<T>();
    }

    #endregion

    #region Methods

    public void Add(T entity)
    {
        _objectSet.AddObject(entity);
    }

    public IEnumerable<T> GetAll()
    {
        return _objectSet;
    }

    #endregion
}

Then create a derived class for each tabel you need to access..

An example (interface and implemtation): Producer is the tables POCO object.

Interface:

    public interface IProducerRepository
{
    Producer GetById(int id);
    void Add(Producer entity);
    IEnumerable<Producer> GetAll();
}

Implementation:

    public class ProducerRepository : BaseRepository<Producer>, IProducerRepository
{
    #region Ctor

    public ProducerRepository(ObjectContext context) : base(context)
    {
    }

    #endregion

    #region Methods

    public Producer GetById(int id)
    {
        return _objectSet.SingleOrDefault(e => e.Id == id);
    }


    #endregion
}

Hope this helps.. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜