2

我一直在努力使用 Breeze 将 SaveChanges 保存到一个投影中,并承认我对 EF 和微风都是新手。之前在尝试使用 WCF 时也有一些类似的问题,但现在我放弃了 WCF 并将 EF 直接添加到我的解决方案中。

在我的控制器中,我将元数据的 DTO 与 DTO 一起返回,并且它完美绑定。

更改客户端上的数据后,我的 Breese 控制器 [HttpPost] SaveChanges(save Bundle) 被调用,并且映射包含 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(全列 - First/EF 逆向工程师/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

非常感谢任何帮助:)

问候,迈克

4

0 回答 0