Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please give me example how replace IModificationCommand to my implementation in MyDbContext #31434

Closed
P9avel opened this issue Aug 9, 2023 · 9 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@P9avel
Copy link

P9avel commented Aug 9, 2023

Sorry for dummy questoin. Can you to show example how replace IModificationCommand to my implementation in MyDbContext?

@roji
Copy link
Member

roji commented Aug 9, 2023

@P9avel what is it exactly that you're trying to accomplish by replacing IModificationCommand?

@P9avel
Copy link
Author

P9avel commented Aug 9, 2023

i am want not send some values to insert. I am now did before SaveChanges follow code

 public static readonly FieldInfo BeforeSaveBehavior = typeof(RuntimeProperty).GetField("_beforeSaveBehavior", BindingFlags.NonPublic | BindingFlags.Instance);
var runtimeProperty = property.Metadata as RuntimeProperty;
                   BeforeSaveBehavior.SetValue(runtimeProperty, isModified ? PropertySaveBehavior.Save : PropertySaveBehavior.Ignore);

I am want to send only changed properties and other not send. I am have isModified flag and need calculate for each object. My code works when saved only 1 object. And i have issue with many rows because my code calls only once before SaeChanges

@roji
Copy link
Member

roji commented Aug 9, 2023

@P9avel I'd strongly advise overriding SaveChanges/SaveChangesAsync in your code, going over the tracked entities, and modifying their state as suits you - this is probably a much easier way to accomplish what you want than to replace IModificationCommand. You can find out more about doing this on this doc page.

@P9avel
Copy link
Author

P9avel commented Aug 9, 2023

I cannot, i think. for example, i have

class Product
{
public int Id {get;set;}
public int Count {get;set;}
}

Now i am want to create var p1 = new Product { Id = 123 }. In my case not need send Count to db because user not set value. I am want to get
insert into Product (id) values(123). In this case ms sql calculated default value for Count. In other case var p1 = new Product { Id = 123, Count = 0 }
In this case need to generate sql insert into Product (id, count) values(123, 0)

@roji
Copy link
Member

roji commented Aug 9, 2023

How is Count database-generated? If it's a computed column, you should be telling this to EF, and it won't send the zero value to the database. Are there any cases where the user does set Count, and you want that to get sent?

@P9avel
Copy link
Author

P9avel commented Aug 9, 2023

How is Count database-generated?
Default
Are there any cases where the user does set Count, and you want that to get sent?
Yes.
In common case, if user set any value (and zero) then need to send to dbms. If user not set value and i am dont need send value to sql. Possible you to show code with replacing IModificationCommand?

@roji
Copy link
Member

roji commented Aug 9, 2023

From your description, Count sounds like a simple column with a default value, as described here in the docs. If you've properly configured it that way (see those docs), EF should not be sending Count to the database when it's equal to 0 (the CLR default).

Note that there is no way to distinguish between new Product { Id = 123 } and new Product { Id = 123, Count = 0 }, the two produce the exact same Product instance; so you cannot vary behavior based on this. If you want to make a distinction here, you can make Count nullable (int?); in that case, if unset it will be null, but the user can set it to 0 to have it sent to the database. However, there really isn't any point in doing if the column default is zero in the database; whether the user sets Count to zero or not - and whether EF sends it to the database or not - yields the same result.

Possible you to show code with replacing IModificationCommand?

I think you're barking up the wrong tree here with IModificationCommand - I haven't seen any reason for you to need that. Can you clearly explain why EF's support for plain old default columns is insufficient?

@P9avel
Copy link
Author

P9avel commented Aug 9, 2023

Sorry in my domain user can set 0 and then need send 0 to dbms. In set method i am update changeProperties collection. and i am can check set user value or not. Other case: import from Excel. In my case when cell is emty then not need send value to dbms. In fact, i am cannot to use null value and cannot to use .HasDefaultValue() because in dbms complex expression and db admin can change default value. Here #12169 wrote Add IColumnModification and IModificationCommand to allow the implementations to be replaced easily. Can you to show example?

@ajcvickers
Copy link
Contributor

As discussed above, IModificationCommand is not the appropriate place to make this change, so showing an example doesn't make sense.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2023
@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants