简体   繁体   English

如果有外键(EF-4),如何使用通用存储库添加新值?

[英]How to add new value with generic Repository if there are foreign keys (EF-4)?

i try to write a kind of generic repository to add method. 我尝试编写一种通用存储库来添加方法。 Everything is ok to add but I have table which is related with two tables with FOREIGN KEY.But Not working because of foreign key 一切都可以添加,但是我有一个表,该表与两个带有FOREIGN KEY的表相关。但是由于外键不起作用

替代文字

public class DomainRepository<TModel> : IDomainRepository<TModel> where TModel : class
{
    private ObjectContext _context;
    private IObjectSet&#60;TModel&#62; _objectSet;

    public DomainRepository(ObjectContext context)
    {
        _context = context;
        _objectSet = _context.CreateObjectSet&#60;TModel&#62;();
    }

    // do something...
    public TModel Add<TModel>(TModel entity) where TModel : IEntityWithKey
    {
        EntityKey key = _context.CreateEntityKey(entity.GetType().Name, entity);          
        _context.AddObject(key.EntitySetName, entity);
        _context.SaveChanges();
        return entity;
    }
    // do something...
}

Calling Repository: 调用存储库:

// insert-update-delete
public partial class AddtoTables
{
    public table3 Add(int TaskId, int RefAircraftsId)
    {
        using (DomainRepository<table3> repTask = new DomainRepository<table3>(new TaskEntities()))
        {
           return repTask.Add&#60;table3&#62;(new table3() { TaskId = TaskId, TaskRefAircraftsID = RefAircraftsId  });
        }
    }
}

How to add a new value if this table includes foreign key relation? 如果此表包含外键关系,如何添加新值?

替代文字

Damn hard to be sure answer is going to be helpful cause I dont think I udnerstand your interface clearly but in order to add objects with foreign keys to generic repositary you will have to add support to add object with foreign keys. 很难确定答案是否会有所帮助,因为我认为我不能清楚地理解您的界面,但是要将带有外键的对象添加到通用存储库中,您将不得不添加支持以添加带有外键的对象。 I think if you have something like params ForeignKey[] somewhere in signatures you will make a step towards having support objects with foreign keys. 我认为,如果签名中某处有params ForeignKey[]类的东西,那么您将朝着具有外键的支持对象迈出一步。 Probably advice is worthless, but I got a worthy advice. 也许建议是毫无价值的,但我得到了有价值的建议。

Read Patterns of Enterprise Application Architecture by Martin Fowler and it will answer all of your questions - in this I am sure. 阅读Martin Fowler撰写的《企业应用程序体系结构的模式》,它将回答您所有的问题-我敢肯定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM