In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Linq data insertion problem how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Linq data insertion problem how to solve" it!
Today, when I insert data with Linq, I always insert an error, saying that a primary key field cannot be empty. After checking for a long time, I feel that the primary key field does not have a null value, which is really depressing.
The table structure to insert data is:
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))
The code to insert the data:
RSSFeedRight feedRight = new RSSFeedRight (); feedRight.UserId = userId; feedRight.FeedId = feedId; feedRight.RightValue = 0; _ Db.RSSFeedRights.InsertOnSubmit (feedRight); _ Db.SubmitChanges ()
Every time it is inserted, it is prompted that FeedId cannot insert a null value, which is depressing, and it is clearly given a non-null value!
Later, after a closer examination, it was found that there were two fields pointing to the UserInfo and RSSFeed tables in this RSSFeedRight entity class, and it gradually became apparent that it was caused by a foreign key setting problem. Immediately search for "linq foreign key insert" via google, and find that many people have the same problem. Find one of the posts, which describes the problem 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.
In other words, we can't set the primary key to be the same as the foreign key, otherwise something will go wrong. Find out what the problem is, and modify the table structure 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,) Thank you for reading. This is the content of "how to solve the problem of Linq inserting data". After the study of this article I believe that we have a deeper understanding of how to solve the problem of Linq insertion data, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.