All the values that I bound in the Register view get set correctly, but I did not bind the navigation properties (Register.Account.AccountType
and Register.Account.Person
) to anything, since I did not know what to do with them.
Now, I can't insert into the database with the above code, because I get a Person foreign key constraint error. It seems that Register.Account
cannot have null values for its Person
or AccountType
navigation properties. Apparently, they must be set (or, at least, the Person
property must be).
I know that I can set these navigation properties manually in the controller. For Person
, I can write something like this before saving to the DB: Register.Account.Person = Register.Person
, and I can likewise come up with something for AccountTypes
to give it its proper value. I've tested this, and it does insert into the database.
But, this doesn't strike me as the right approach. It seems to me that there must be a better, more proper way of clarifying the model or table relationships to .NET before inserting into the database.
Does anybody know a better way?
P.S.: I'm using .NET 6.
CodePudding user response:
Per Jeremy's suggestion, I solved this problem by creating a new View Model, which only included the properties that I needed to bind, and omitting any navigation properties that weren't necessary to insert into the database successfully.