在DTO上的Breeze EF SaveChanges()

时间:2013-08-15 21:32:11

标签: entity-framework breeze dto

我一直在努力与Breeze进行SaveChanges的投射,并承认我对EF和微风都是新人。当我尝试使用WCF时,之前有一些类似的问题,但现在我已经放弃了WCF并将EF直接添加到我的解决方案中。

在我的控制器中,我将元数据的DTO与DTO一起返回,它完美地结合在一起。

在更改客户端上的数据后,我的Breese控制器[HttpPost] SaveChanges(保存Bundle)被调用,map包含DTO和更改。

如何坚持更改?如果我重新读取DTO投影以进行更新,那么EF无法保存投影,因为它没有“跟踪”,如果我读取完整实体,那么Breeze错误“序列包含没有匹配元素”,因为它正在寻找DTO?我想使用AutoMapper吗?

控制器:

[BreezeController]
public class BreezeController : ApiController
{

   readonly EFContextProvider<ManiDbContext> _contextProvider = new EFContextProvider<ManiDbContext>();

    [HttpGet]
    public string Metadata()
    {
        return  _contextProvider.Metadata();
    }

    [HttpGet]
    public IQueryable<ConsigneDTO> Consignee(string refname)
    {
        return _contextProvider.Context.consigneDTO(refname);
    }

    [HttpPost]
    public SaveResult SaveChanges(JObject saveBundle)
    {
        ManiDbContextProvider _mcontextProvider = new ManiDbContextProvider();
        return  _mcontextProvider.SaveChanges(saveBundle);
    }

ManiDbContext(主要的DBContext是CifContext,它是Database First / EF逆向工程师)

 public class ManiDbContext  : DbContext 
 {
    public CifContext CifDbContext = new CifContext();


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
        Database.SetInitializer<ManiDbContext>(null);
        modelBuilder.Configurations.Add(new ConsigneDTOMap());
    }

    public override int SaveChanges()
    {
        CifDbContext.SaveChanges();
        return 1;
    }

    public IQueryable<ConsigneDTO> consigneDTO(string refname)
    {
        IQueryable<ConsigneDTO> q = this.CifDbContext.Consignes
         .Where(x => x.Refname == refname)
         .Select(f => new ConsigneDTO {Refname = f.Refname, Consignee = f.Consignee, Address1 = f.Address1, Address2 = f.Address2, Address3 = f.Address3});

        return q;
    }

ManiDbContextProvider

    public class ManiDbContextProvider : EFContextProvider<CifContext>
//  public class ManiDbContextProvider : EFContextProvider<ManiDbContext>
  {
    public ManiDbContextProvider() : base() { }

    protected override void OpenDbConnection()
    {// do nothing
    }

    protected override void CloseDbConnection()
    { // do nothing 
    }

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        var entity = entityInfo.Entity;

        if (entity is ConsigneDTO)
        {
            return BeforeSaveConsignee(entity as ConsigneDTO, entityInfo);
        }
        throw new InvalidOperationException("Cannot save entity of unknown type");
    }

     private bool BeforeSaveConsignee(ConsigneDTO c, EntityInfo info)
    {

        var consdata = this.Context.CifDbContext.Consignes 
             .Where(x => x.Refname == c.Refname)
             .FirstOrDefault();   // ENTITY 

      //  var consdata = this.Context.consigneDTO(c.Refname); // DTO 

       return (null != consdata) || throwCannotFindConsignee();
    }

CifContext(Full Columns - First / EF Reverse Engineer / Consigne类包含键)

  public partial class CifContext : DbContext
  {
    static CifContext()
    {

        Database.SetInitializer<CifContext>(null);
    }

    public CifContext()
        : base("Name=CifContext")
    {
    }

    public DbSet<Consigne> Consignes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();   // Use singular table names
        Database.SetInitializer<CifContext>(null);

        modelBuilder.Configurations.Add(new ConsigneMap());
     }

无论我是读实体还是DTO - 我对微风如何更新EF一无所知

任何帮助都非常感谢:)

此致 麦克

0 个答案:

没有答案