A problem during data insertion by using Linq to SQL

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.