Linq to sql 重复条目

Linq to sql 重复条目,linq-to-sql,model-view-controller,Linq To Sql,Model View Controller,我已经创建了一个universalrepository,它接受传递给它的类型,当我创建数据输入方法时,实体创建得很好,但是当我创建一个链接到它的实体时,我会再次创建基本实体。你知道为什么吗 细节 我已经将一个规范划分为多个表来管理内容 现在我有一个个人实体,一个申请实体…(实际上申请者和个人是一样的),一个承包商实体。承包商只能由申请人创建,因此始终会创建申请人,因此始终会创建人员 当我继续创造一个人时,它创造了一个人,这很好,但当我创造一个申请人时,它又创造了一个人。同样,当我创建一个承包商时

我已经创建了一个universalrepository,它接受传递给它的类型,当我创建数据输入方法时,实体创建得很好,但是当我创建一个链接到它的实体时,我会再次创建基本实体。你知道为什么吗

细节

我已经将一个规范划分为多个表来管理内容

现在我有一个个人实体,一个申请实体…(实际上申请者和个人是一样的),一个承包商实体。承包商只能由申请人创建,因此始终会创建申请人,因此始终会创建人员

当我继续创造一个人时,它创造了一个人,这很好,但当我创造一个申请人时,它又创造了一个人。同样,当我创建一个承包商时,出于某种原因,它会创建一个人和多个申请人

这是我对SQL的LINQ。如果你注意到我可以改进这段代码,我也会很感激的

这是存储库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace ParkingPermit.Models
{
    public class UniversalManagerRepository<T> :IRepositoryT<T>
                                               where T:class
    {
        private Table<T> _table;
        private readonly DB _db ;//= new DB();

        public UniversalManagerRepository()
        {
            _db = new DB();
            _table = _db.GetTable<T>();
        }

        #region IRepositoryT<T> Members

        public T Create(T create)
        {

           // _table = new DB().GetTable<T>();

            //_db.GetTable(typeof(T)).InsertOnSubmit(create);

            _table.InsertOnSubmit(create);
            Save();

            return create;
        }

        public void Delete(T delete)
        {
            throw new NotImplementedException();
        }

        public T Edit(T edit)
        {
            throw new NotImplementedException();
        }

        public T GetItem(int id)
        {
            throw new NotImplementedException();
        }

        public T Update(T update)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<T> List()
        {
            //IQueryable i = _db.GetTable(typeof(T)).AsQueryable()  ;
            return _db.GetTable(typeof(T)) as IEnumerable<T>;
            //throw new NotImplementedException();
        }

        public void Save()
        {
            //_db.SubmitChanges();
            _table.Context.SubmitChanges();
            //throw new NotImplementedException();
        }

        #endregion
    }
}

我已经解决了这一部分,显然它现在发布了更新,安博迪能发现任何不寻常的事情吗

  • 我的第一个问题的答案是,在模型绑定中,实体从会话中被操纵,实体被创建回服务层
  • 显然,因为它发生在linq orm框架之外,所以需要将该实体重新创建为“db中的From子句…From..”,然后linq正确地识别它并执行正确的插入工作


    有人能帮我更新/编辑吗..请

    问题是,当添加申请者并从会话中分配申请者时,它会创建一个新的人,实际上是一开始创建的原始人。我怎样才能避免这种情况。
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)  
    {  
       var personType = (Person)controllerContext.HttpContext.Session[PersonSessionKey];  
    
       controllerContext.HttpContext.Session[CurrentApplicantSessionKey] = null;  
    
       var av = new ApplicantValidator(new            ModelStateWrapper(bindingContext.ModelState));  
       var newApplicant = bindingContext.Model as Applicant;  
    
    
    
    
       if (personType == null)  
       {  
    
           bindingContext.ModelState.AddModelError(bindingContext.ModelName,  
    
                                                   "Cannot Update this Instance directly, please restart the application");  
          // controllerContext.HttpContext.Session[PersonSessionKey] = personType;  
       }  
       else if (newApplicant != null)  
       {  
           if (newApplicant.Person != null)  
           {  
               if (newApplicant.Person.Equals(personType as Person))  
               {  
                   bindingContext.ModelState.AddModelError(bindingContext.ModelName,  
                                                           "A person with these details already exists, please restart the application...");  
                   //return  
                   controllerContext.HttpContext.Session[PersonSessionKey] = null;  
                   personType = null;  
               }  
           }  
           else if (av.Validate(newApplicant))  
           {  
    
               if (newApplicant.Person == null)  
               {  
    
                   newApplicant.Person = personType as Person;  
                   newApplicant.PersonId = personType.PersonId;  
    
    
               }  
           }  
    
       }  
    
    }