开发者

Interface inheritance in Entity Framework

Using Entity Framework 4 I want to create a base interface for my objects so that the properties of the base interface get implemented as fields in the table for each derived class (not in its own table), and then deal with the derived classes using the interface.

For example, have an interface and some classes like so:

public interface IBaseEntity
{
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
}

public class SomeEntity : IBaseEntity
{
    public int SomeEntityId { get; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
}

public class OtherEntity : IBaseEntity
{
    public int OtherEntityId { get; }
    public float Amount { get; set; }
    public DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
}

This would result in two tables in the database, SomeEntity and OtherEntity, which would have four fields each. SomeEntity has SomeEntityId, Name, CreatedOn and CreatedBy and OtherEntity has OtherEntityId, Amount, CreatedOn and CreatedBy. There is no IBaseEntity table.

I would expect to see this displayed in the designer as IBaseEntity being an abstract entity with the CreatedOn and CreatedBy properties and the two concrete entities having just their non-derived properties - so SomeEntity just has So开发者_开发知识库meEntityId and Name. There is an inheritance relationship between the concrete entities and the abstract entity.

Then I would like to have automatic column updates for these objects when saving them, like so:

namespace MyModel
{

    public partial class MyEntities
    {
        partial void OnContextCreated()
        {
            this.SavingChanges += new EventHandler(OnSavingChanges);
        }

        private static void OnSavingChanges(object sender, EventArgs e)
        {
            var stateManager = ((MyEntities)sender).ObjectStateManager;
            var insertedEntities = stateManager.GetObjectStateEntries(EntityState.Added);

            foreach (ObjectStateEntry stateEntryEntity in insertedEntities)
            {
                if (stateEntryEntity.Entity is IBaseEntity)
                {
                    IBaseEntity ent = (IBaseEntity)stateEntryEntity.Entity;
                    ent.CreatedBy = HttpContext.Current.User.Identity.Name;
                    ent.CreatedOn = DateTime.Now;
                }
            }
        }
    }
}

I am just starting out with Entity Framework and it seems like this should be able to be done fairly easily but how to actually implement it is escaping me. Am I way off track here or is this sort of thing possible in Entity Framework 4? The Table Per Concrete Type Strategy seems like the solution but I haven't been able to get it working.


The interface won't be part of your entity model and can't be displayed in the designer. But you can add it via a partial class, and then your code will work. We actually use a T4 template for this, but it works fine when done manually, too.


Well, this is pretty darned old but I thought I'd mention that you can accomplish what the original poster wants through abstract classes as well as the way the accepted answerer suggested.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜