zzzprojects / EntityFramework-Plus

Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more
https://entityframework-plus.net/
MIT License
2.25k stars 318 forks source link

Bug: Batch Update with Inheritance #238

Open novamxd opened 7 years ago

novamxd commented 7 years ago

Hello,

I have a base modal class "SystemObject" which has a bunch of common properties of all my models (like Created Date, OwnerId, etc) stored in a table SystemObject. All my other models store their data in their separate tables.

Z.EntityFramework.Plus.EF6 Version: 1.6.21

Example:

[Table("SystemObject")]
public class SystemObject
{
    public Guid Id {get; set;}

    public DateTime Created {get;set;}

    public bool Deleted {get;set;}
}

[Table("User")]
public class User : SystemObject
{
    public string Username {get;set;}
}

If I try to update a SystemObject column when using the Update() extension on the child class User, it fails with an exception: The destination column could not be found:{column}. The offending line in the code is 911 of shared\Z.EF.Plus.BatchUpdate.Shared\BatchUpdate.cs.

Example:

using(MainContext mcContext = new MainContext())
{
    //mark "TestUser" as deleted
    mcContext
        .Users
            .Where(i => i.Username == "TestUser")
            .Update(i => new User() 
            { 
                Deleted = true 
            });
}

Without digging into it, it sounds like the ScalarProperties collection doesn't include inherited properties, so either that needs to include them or it needs to look through base classes. Can I get a fix for this?

Thanks!

novamxd commented 7 years ago

Also using the batch Delete function under this same model seems to produce a null object reference error within the Entity Framework Plus.

JonathanMagnan commented 7 years ago

Hello @novamxd ,

Inheritance such as TPC, TPT are currently limitation to this functionality.

Our library doesn't support it.

While we plan and hope to support it in the V2.x version, we currently have no short term plan to support it due to the complexity.

Best Regards,

Jonathan

novamxd commented 7 years ago

Hi Jonathan,

Thanks for the clarification. I'll need to find a temporary solution until 2.x comes out!

Regards

JonathanMagnan commented 7 years ago

I will reopen it to make it easier to notify you if we start to make some progress on this request.

Btw, which database provider are you using?

Best Regards,

Jonathan

novamxd commented 7 years ago

Hi Jonathan,

MSSQL. Also I decided to add the functionality myself. I can now use BatchUpdate and BatchDelete given the situation I outlined above. I have the unit tests I wrote working for EF5 and EF6 but I cannot for the life of me get EFCore to compile in VS2017. Is there something I'm missing? It looks like the projects cannot locate the .NET Standard library so long as the shared libraries are attached to the project.

Regards

JonathanMagnan commented 7 years ago

Hello @novamxd ,

The GitHub version may have some issue perhaps. GitHub is not the real source control for Entity Framework Plus, I use Visual Studio Online.

I get the same error as you when I open it through NuGet, I will try to fix it later.

Do you think you could submit a Merge? I was planning to look at it soon, but if you already did it, that will save me some previous time ;)

Best Regards,

Jonathan

novamxd commented 7 years ago

Hi Jonathan,

Sure I can do a commit. Though since I can't get the EFCore stuff working, the patch has only been applied to the EF5-6 code. Did you want to fix the issue in GitHub first so I can finish the implementation? Or did you just want to get the EF5-6 patch?

Regards

novamxd commented 7 years ago

Or alternatively I could give you my VS account and I could apply the update there. Up to you.

JonathanMagnan commented 7 years ago

Hello @novamxd ,

Try to push it, I believe it's the easiest solution on my side. I will review your change and fix/test the EF Core on my side.

Thank a lot for the help.

Best Regards,

Jonathan

novamxd commented 7 years ago

Hi Jonathan,

Ok. I'll take care of that this weekend.

Regards

JonathanMagnan commented 7 years ago

Great ;)

Thank a lot

Best Regards,

Jonathan

novamxd commented 7 years ago

Hey Jonathan,

So I got it working with EFCore .NET 4.5.2. I'm just seeing if I can get the .NETStandard stuff working... or not.

This basically sums my progress so far: 1-af4l0wqv77wpd1mw3r530q

;)

JonathanMagnan commented 7 years ago

lol ;)

Well, I was waiting you to merge the code before trying to fix the .NET core unit test on GitHub.

Don't bother with EF Core, you already made a lot of job with EF6

Best Regards,

Jonathan

novamxd commented 7 years ago

Hi Jonathan,

I created a pull request with the changes. This would be the first time I've used Git Hub for this purpose, so hopefully it works for you. If not, let me know.

Regards