Entity Framework with Nullable Guid? causing Validation Error

c# entity-framework entity-framework-6

Popular Answer

I'll tackle this somewhat differently in my response and attempt to explain what needs to be altered in order to provide a useful example;

Unique IDs must not be nullable, as someone has already said, therefore theId area inDomainType ought to aGuid . Additionally, you must modify the Id column inDomainCustomField These could need a blank constructor only to generate a new Guid.

The second thing is the foreign key in your other item.Guid? DomainTypeId is OK, this can remain, but you'll need to alter your setup if it does.

You have said that the property is necessary in this block.

modelBuilder.Domain<DomainType>()
                .HasMany(p => p.DomainCustomFields)
                .WithRequired()
                .HasForeignKey<Guid?>(x => x.DomainTypeId);

Make it optional, please;

modelBuilder.Domain<DomainType>()
                .HasMany(p => p.DomainCustomFields)
                .WithOptional()
                .HasForeignKey<Guid?>(x => x.DomainTypeId);

That ought to put your problems to rest. Please ask me any questions.

1
2/17/2017 10:30:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow