Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据实体插入:已添加具有相同键的项_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 数据实体插入:已添加具有相同键的项

C# 数据实体插入:已添加具有相同键的项,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我知道这个问题已经被问过很多次了,在阅读了文章之后,我相信它与实体名称有关,但我对数据实体模型是完全陌生的,当我尝试用博客详细信息简单插入博客条目时,我遇到了这个问题 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and w

我知道这个问题已经被问过很多次了,在阅读了文章之后,我相信它与实体名称有关,但我对数据实体模型是完全陌生的,当我尝试用博客详细信息简单插入博客条目时,我遇到了这个问题

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: An item with the same key has already been added.

Source Error:

Line 365:    public void AddToblogs(blog blog)
Line 366:    {
Line 367:        base.AddObject("myblogs", blog);
Line 368:    }
Line 369:


Source File: C:\Inetpub\int422_113b16\webcontent\App_Code\blogModel.cs    Line: 367

Stack Trace:

[ArgumentException: An item with the same key has already been added.]
   System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
   System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +9382923
   System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.LoadRelationshipTypes() +661
   System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.LoadTypesFromAssembly() +17
   System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load() +25
   System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.Load() +4
   System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData) +160
   System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage, Object& loaderCookie, Dictionary`2& typesInLoading, List`1& errors) +166
   System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +316
   System.Data.Metadata.Edm.ObjectItemCollection.ImplicitLoadAssemblyForType(Type type, EdmItemCollection edmItemCollection) +84
   System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadAssemblyForType(Type type, Assembly callingAssembly) +151
   System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity) +211
   blogEntities.AddToblogs(blog blog) in C:\Inetpub\int422_113b16\webcontent\App_Code\blogModel.cs:367
   BlogManager.BlogAdd(String title, String content, String userName) in C:\Inetpub\int422_113b16\webcontent\App_Code\BlogManager.cs:27
   Project_Admin_create_post.post_Click(Object sender, EventArgs e) in C:\Inetpub\int422_113b16\webcontent\Project\Admin\create-post.aspx.cs:25
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
BlogManager.cs

public class BlogManager
{
    private blogEntities _dbEnt = new blogEntities();

    public BlogManager()
    {
    }

    public void BlogAdd(int numId, string title, string content,string userName) 
    {

        blog newPost = new blog();
        newPost.blog_id = 1;
        newPost.blog_title = title;
        newPost.blog_content = content;
        newPost.date_created = null;
        newPost.user_name = userName;

        _dbEnt.AddTotheblog(newPost);
        _dbEnt.SaveChanges();
    }

    public class NameNotUniqueException : Exception
    {
        public NameNotUniqueException(string msg = "INT422 ERROR: matching data already exists")
            : base(msg)
        { }
    }
}
创建-post.cs

public partial class Project_Admin_create_post : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            blogStatus.Text = "Your post is published";
            title.Enabled = false;
            content.Enabled = false;
        }
    }

    protected void post_Click(object sender, EventArgs e)
    {
        string _userName = User.Identity.Name;

        BlogManager blogMgr = new BlogManager();
        blogMgr.BlogAdd(title.Text, content.Text, _userName);
    }
}

我认为错误信息告诉了你真相。以前,您已经在博客文章的子对象中,或者博客文章本身添加了另一个具有相同实体键的此类实体


是否有可能在您的某个表中忘记设置PK的标识键?如果是这样的话,您可能有一个键为0的行,随后的插入与之冲突。

我认为错误消息在这里告诉您真相。以前,您已经在博客文章的子对象中,或者博客文章本身添加了另一个具有相同实体键的此类实体

if(blog.EntityState==EntityState.Detached)
    base.AddObject("myblogs", blog);


是否有可能在您的某个表中忘记设置PK的标识键?如果是这样的话,您可能有一个键为0的行,后续插入与之冲突。

代码将使用我用于插入的文件进行更新。如果您正在寻找任何其他特定文件,请告诉我一切都是一样的。。我有PK,身份密钥&我手动添加了号码。。问题仍然存在,您是否在使用POCO?当你说:
newblog()
-构造函数是否创建了任何可能会把事情搞砸的子对象?POCO?不知道。。从数据实体开始,所以。。但是如果我不创建新对象,我将无法传递值。。还有其他方法吗?这些类是在您将表拖到设计器上时自动创建的,还是手动编写的?代码将使用我用于插入的文件进行更新。如果您正在寻找任何其他特定文件,请告诉我一切都是一样的。。我有PK,身份密钥&我手动添加了号码。。问题仍然存在,您是否在使用POCO?当你说:
newblog()
-构造函数是否创建了任何可能会把事情搞砸的子对象?POCO?不知道。。从数据实体开始,所以。。但是如果我不创建新对象,我将无法传递值。。还有其他方法吗?这些类是在您将表拖到设计器上时自动创建的,还是手动编写的?什么是blog PK?你忘了给它赋值了吗?它的blog\u id。。不是自动分配..而是手动分配。。。检查更新的BlogAdd(..)Debug方法代码,以确保BlogAdd方法未被调用两次,是否在添加到上下文之前确定上下文中不存在blog_id=1的日志?我希望我可以。。学校服务器不允许,但我刚刚下载了整个网站。。我要在本地服务器上调试什么是博客PK?你忘了给它赋值了吗?它的blog\u id。。不是自动分配..而是手动分配。。。检查更新的BlogAdd(..)Debug方法代码,以确保BlogAdd方法未被调用两次,是否在添加到上下文之前确定上下文中不存在blog_id=1的日志?我希望我可以。。学校服务器不允许,但我刚刚下载了整个网站。。我要在本地服务器上调试
if(blog.EntityState==EntityState.Detached)
    base.AddObject("myblogs", blog);