When adding a new record, the Key Attribute property does not save it’s value to the DB.
Further investigation showed me that it’s not in the SQL statement at all.
How can I add records with a value to the Key column?
EDIT:
This is the table:
CREATE TABLE `members` (
`UniqueID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`MemberID` BIGINT(20) NOT NULL,
PRIMARY KEY (`UniqueID`) USING BTREE,
UNIQUE INDEX `Index_MemberID` (`MemberID`) USING BTREE);
This is the entity:
public class Member
{
public long UniqueId { get; set; }
[Key]
public long MemberId { get; set; }
}
Your mapping is wrong:
You mark a property as key in your model whose corresponding column is not the primary key in the database.
You define your key property
MemberIdas database generated (bacuse it’s default and you don’t switch it off) but the column in the database isn’t an autoincrementing identity.The second problem you can solve by switching identity off:
The first point leads to the behaviour that your supplied values of
UniqueIdin the model will be overwritten by the autoincrementing values in the database. If there will be other problems of mapping a key property to a non-key column with a unique column value constraint… I don’t know. At least I think that this is a unofficial, not supported usage of the key attribute and kind of mapping.