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

DatabaseGeneratedOption.Computed always causes null #62

Closed
robert-j-engdahl opened this issue Jan 16, 2017 · 24 comments
Closed

DatabaseGeneratedOption.Computed always causes null #62

robert-j-engdahl opened this issue Jan 16, 2017 · 24 comments
Assignees

Comments

@robert-j-engdahl
Copy link

I have a column like the following:

    [Column, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? LastModified { get; set; }

and a SQL trigger that sets it as expected. Entity framework expects the database to set this property and reads it back after each insert and update. Effort always reports this as null. That is:

    var entity = new MyEntity { LastModified = DateTime.Now };
    dbContext.MyEntities.Add(entity);
    dbContext.SaveChanges();
    Assert.NotNull(entity.LastModified);

fails where dbContext is a DbContext created from a transcient Effort DbConnection.

I would like some mechanism to get around this. Perhaps an EntityWithComputedColumnInserting event and an EntityWithComputedColumnUpdating event for allowing users to handle the computing of computed columns. Say using an EventArgs similar to the following:

    public class ComputedColumnEventArgs : EventArgs
    {
        public Type EntityType { get; }
        public string ComputedColumnName { get; }
        public object ComputedValue { get; set; }
    }

I could then make an event handler like the following in my test class:

    private void ComputeLastModified(object sender, ComputedColumnEventArgs e)
    {
        if (e.EntityType = typeof(MyEntity) 
            && e.ComputedColumnName == nameof(MyEntity.LastModified))
        {
            e.ComputedValue = _now;
        }
    }
@pfaustinopt
Copy link

I'm facing the same situation at the moment. I'm going to download the source code and try to find if it's possible to implement the solution you are suggesting.

@robert-j-engdahl
Copy link
Author

Thats the spirit!

@pfaustinopt
Copy link

I took a look on the code and I did find some references to computed columns. However I couldn't catch the moment that the computed column is set to null. Maybe some contributors can give us some feedback? @JonathanMagnan

@JonathanMagnan
Copy link
Member

Hello @pfaustinopt ,

Yes, we will be happy to look at it at the same time of your other problem.

Do you think you could provide us a test project with this issue?

It will make easier/faster for my developer to getting started for investigating it.

We now always ask for a project since we found out that most issues are missing some essential information or are resolved by the requestor when creating it

(Even if the issue seem very easy to reproduce, by getting a test project, it allow us to give a faster support and better experience for the support of all our free libraries)

Best Regards,

Jonathan

@pfaustinopt
Copy link

Here you have it.

UnitTestProject1.zip

@JonathanMagnan
Copy link
Member

Great thank ;)

@JonathanMagnan JonathanMagnan self-assigned this Apr 16, 2018
@JonathanMagnan
Copy link
Member

Hello @pfaustinopt ,

After some time passed on this issue, unfortunately, I don't think we can do anything for this moment.

There is currently no trigger logic implemented on Effort which make it hard to support Computed for DateTime value.

Best Regards,

Jonathan

@5p1k3md
Copy link

5p1k3md commented Jul 10, 2018

Hello,

i'm facing the same issue.
If i try to update or create a new entitiy, which holds a column with the [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute i get the error message, that the columns value can not be null.
It doesn't matter if i pass a value or not, the error message resists until i delete the attribute.

Maybe you can have a look at it?

Best regards

@manojItela
Copy link

facing the same issue

@JonathanMagnan
Copy link
Member

Hello @manojItela ,

We recently started to work on this project (We want to clean most issues/pull in September), we will very soon look how to handle this scenario since that's very common.

Best Regards,

Jonathan

@5p1k3md
Copy link

5p1k3md commented Jan 10, 2019

We're facing the same problem since almost a half year. Will this be fixed in future releases?

Best regards

@JonathanMagnan
Copy link
Member

Hello @5p1k3md ,

We hope that it will eventually be fixed but unfortunately, this one is complex to fix and our time is very limited when supporting free library ;(

@5p1k3md
Copy link

5p1k3md commented Jan 15, 2019

Hello @JonathanMagnan,

can you give us a vague assumption if it will be fixed in 1 month or 1 year?

Thanks in advance.

Best Rregards.

@JonathanMagnan
Copy link
Member

We will probably not work on it in the next following month, so since we have no date, let say 1 year

@drameryan
Copy link

drameryan commented Mar 3, 2019

Is there any solution to this problem? I do not need that the value of the computed column to be correct, I just need that I can add values without crashing. Can we exclude the computed column?

@JonathanMagnan
Copy link
Member

Hello @drameryan ,

We will look at it. Adding an option to exclude those computed columns could be a perfect workaround.

Thank for your suggestion.

Best Regards,

Jonathan

@JonathanMagnan
Copy link
Member

Hello @drameryan ,

Do your computed column value is a key? We tried several column types but we are not able to reproduce the columns value can not be null error. Column are always saved with their default value, and doesn't make our application crash

@drameryan
Copy link

Hi @JonathanMagnan ,

No, it's a DB First model, and the calculated column was simply Fullname, computed as Name + ' ' + Surname. So it doesn't have a default vale.

Default Value -> not defined
Entity Key -> false
Nullable -> false
StoreGeneratedPattern -> Computed
Type -> String

@JonathanMagnan
Copy link
Member

Hello @drameryan ,

It's possible for you to provide a project sample?

It will make easier for my developer to get started to fix this error. I highly believe something would be possible in this case.

@drameryan
Copy link

drameryan commented Mar 7, 2019

I think I isolated the case. It's DB First model.

I created 3 test cases, first table with no computed column, Id, Name, Surname
Second case [TableWithComputedNullable]: Id, Name NULL, Surname NULL, Fullname
Third case [TableWithComputedNonNullable]: Id, Name NOT NULL, Surname NOT NULL, Fullname

The first 2 cases works as expected.
In the second case, Name and Surname are nullable, therefore Fullname also not null. So a null value of the full name does not cause a crash.

But, in the third case, Fullname also automatically becomes NOT NULL, and the default Null value causes the crash NMemory.Exceptions.ConstraintException: Column 'Fullname' cannot be null. Error code: GenericError

Here is my project folder, you can find also the sql create table scripts inside.
https://startuappdev.blob.core.windows.net/web/ComputedColumn.zip

@JonathanMagnan
Copy link
Member

Thank a lot for the project @drameryan ,

Using your project, we easily find a quick fix.

We will re-investigate our fix tomorrow to try to improve it.

Unless something goes wrong, you can expect a new release next Monday.

Best Regards,

Jonathan

@drameryan
Copy link

That's great! Thank you very much.

Regards

@JonathanMagnan
Copy link
Member

Hello @drameryan ,

The v2.1.0 has been released.

A default value (or empty string) will be inserted when you turn on the following options EntityFrameworkEffortManager.UseDefaultForNotNullable

We are currently investigating for a better fix but meanwhile this one work with the example you provided.

Best Regards,

Jonathan

@drameryan
Copy link

@JonathanMagnan that's great, I checked and confirm that it works as expected! Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants