BlueshiftSoftware / EntityFrameworkCore

NoSQL providers for EntityFramework Core
Other
281 stars 57 forks source link

bug: can not modify child #33

Closed SarcoZ closed 5 years ago

SarcoZ commented 5 years ago

[MongoDatabase("fengpintech")] public class MongoContext : DbContext {
public DbSet Customers { get; set; }

    public MongoContext()
       : this(new DbContextOptions<MongoContext>())
    {

    }

    public MongoContext(DbContextOptions<MongoContext> dataCenterContextOptions)
       : base(dataCenterContextOptions)
    {

    }       
}

public class Customer { ///

/// Key /// [BsonId] [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public ObjectId Id { get; private set; }

    public string EnterpriseName { get; set; }

    public List<Aptitude> Aptitudes { get; set; } = new List<Aptitude>();
}

[ComplexType]
public class Aptitude
{       
    public string CertificateName { get; set; }

    public string IssuingAgency { get; set; }
}

my test code is:

class Program { static void Main(string[] args) { var services = new ServiceCollection(); services.AddDbContext(options => options.UseMongoDb("mongodb://localhost"));

        var sp = services.BuildServiceProvider();
        var context = sp.GetService<MongoContext>();

        var customer = new Customer { EnterpriseName = "ROZH" };
        context.Customers.Add(customer);
        context.SaveChanges();

        var c1 = context.Customers.FirstOrDefault(p => p.EnterpriseName == "ROZH");
        c1.Aptitudes.Add(new Aptitude { CertificateName = "AA", IssuingAgency = "XHB" });
        var m = context.Update(c1);
        var s = m.State;
        context.SaveChanges(acceptAllChangesOnSuccess:true);            
        Console.ReadKey();
    }
}

customer save success,then I add a child Aptitudes instance,but can not save

crhairr commented 5 years ago

It would appear that I never updated UpdateOneModelFactory after the implementation of complex types. The result is that the method that builds property update definitions does not list any sub-document changes.

I'm putting in a change to build out those navigations, as well as to ensure that updates to non-owned/complex-type navigations (ie: the foreign key id) are also updated if a relationship changes.

These changes will hopefully also allow for push/pull of collections items.

crhairr commented 5 years ago

I have a partial fix for this. I had to add a bunch of query tracking logic to attach the full graph, including "owned" child documents, to the state manager. Now it can actually see all of the modified sub-documents. I'm going to work tonight on making the actual update logic a little bit more robust: currently, it creates an update graph that spans the document from the root to each updated leaf node. I'd like to make that a little bit more concise and correctly use the update definitions to partially update sub-document children, and remove/add collection elements using MongoDb's syntax.

crhairr commented 5 years ago

I apologize for how long this particular issue has taken. I started trying to fix this the cheap way, and then decided to do it the right way and ended up effectively re-writing the entire metadata system, half the MongoDB Linq adapter, most of the document conventions, and a bunch of other small issues. I don't get a lot of time to work on the provider, so it's taken me a month and a half of a couple of hours here and there.

I have a branch with the current set of changes. I need to tweak a few more things to get the Identity tests to pass, but this issue is almost fixed. Please bear with me a little longer.

crhairr commented 5 years ago

I finally just got the last bit of this done. This reflects an effectively complete overhaul of the provider for the nearly sole purpose of tracking subdocument modifications.

SarcoZ commented 5 years ago

@crhairr Good,T will try later

maulikdoshi82 commented 5 years ago

Does that mean framework can go on release instead of pre-release?

crhairr commented 5 years ago

Unfortunately not.

EFCore 3.0 fixes a lot of the underlying issues preventing toss MongoDB provider from being production worthy. I was working on that update when my wife's health developed some complications. We are now in the middle of a cross-country move to address that, and this project has taken a bit of a backseat.

I would be happy to review and accept pull requests when things start to settle back down.