Copy codeThe Code is as follows:
Create table RSSFeedRight
(
FeedId int Foreign Key (FeedId) References RSSFeed (FeedId) not null, -- FeedId,
UserId int Foreign Key (UserId) References UserInfo (UserId) not null, -- UserId,
RightValue bigint not null Primary key (UserId, FeedId ),
)
Code for inserting data
RSSFeedRight feedRight = new RSSFeedRight ();
FeedRight. UserId = userId;
FeedRight. FeedId = feedId;
FeedRight. RightValue = 0;
_ Db. RSSFeedRights. InsertOnSubmit (feedRight );
_ Db. SubmitChanges ();
Each insert operation prompts that FeedId cannot insert a null value. If it is depressing, it clearly gives a non-null value!
Later, I checked carefully and found that there were two fields pointing to the UserInfo and RSSFeed tables in this RSSFeedRight object class. Later, I gradually felt that it was caused by a foreign key setting problem. Search "linq foreign key insert" by google Now"
Many people have encountered the same problem.
Find one of the posts
Http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e14f76ec-0ffe-4dd1-893c-6a4c8440c54a
The problem is described as follows:
The mapping information (Assocation attribute on Table1 & Table2) has the foreign key dependency going in the wrong direction. it's claiming that the primary-key in table1 (the one that is auto-incremented) is a foreign key to the primary key in table2. You want that just the opposite. you can change this in the designer, DBML file or directly in the code (for a quick test) by changing IsForeignKey value for both associations.
That is to say, we cannot set the primary key to be the same as the foreign key, otherwise there will be problems. Locate the problem and make the following changes to the table structure.
Copy codeThe Code is as follows:
Create table RSSFeedRight
(
Id int identity (1, 1) not null Primary Key,
FeedId int Foreign Key (FeedId) References RSSFeed (FeedId) not null, -- FeedId,
UserId int Foreign Key (UserId) References UserInfo (UserId) not null, -- UserId,
RightValue bigint not null,
)
Solve the problem.
When veterans encounter new problems, the technology will be aging if it is not updated frequently.