Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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#_Entity Framework 5_Poco - Fatal编程技术网

C# 使用实体不保存对象';行不通

C# 使用实体不保存对象';行不通,c#,entity-framework-5,poco,C#,Entity Framework 5,Poco,我有这样设计的桌子 Create table [dbo].[Category]( [Id] [int] IDENTITY(1,1) NOT NULL, [ShortString] varchar(100), [Description] varchar(100), CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF

我有这样设计的桌子

Create table [dbo].[Category](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ShortString] varchar(100),
[Description] varchar(100), 
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
我用t4模板将其转换为POCO类

namespace ArabicEWorld.Model
{
    using System;
    using System.Collections.Generic;

    public partial class Category
    {
        public Category()
        {
            this.Nouns = new HashSet<Noun>();
        }

        public int Id { get; set; }
        public string ShortString { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Noun> Nouns { get; set; }
    }
}
public abstract class Repository<T> : IRepository<T>, IDisposable where T : class
    {

        private ClassesDatabasdEntities Context;
        protected Repository()
        {
            Context = new ClassesDatabasdEntities ();
        }
 public void Commit()
        {
            Context.SaveChanges();
        }
        public void Add(T entity)
        {
            Context.CreateObjectSet<T>().AddObject(entity);
        }
        public void Update(T entity)
        {
            Context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            Context.SaveChanges();
        }
        public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }
        public void Dispose()
        {
            if (Context != null)
            {
                Context.Dispose();
            }
            GC.SuppressFinalize(this);
        }
}
名称空间ArabicEWorld.Model
{
使用制度;
使用System.Collections.Generic;
公共部分类
{
公共类别()
{
this.nomes=newhashset();
}
公共int Id{get;set;}
公共字符串短字符串{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection名词{get;set;}
}
}
现在我有了一个

namespace ArabicEWorld.Model
{
    using System;
    using System.Collections.Generic;

    public partial class Category
    {
        public Category()
        {
            this.Nouns = new HashSet<Noun>();
        }

        public int Id { get; set; }
        public string ShortString { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Noun> Nouns { get; set; }
    }
}
public abstract class Repository<T> : IRepository<T>, IDisposable where T : class
    {

        private ClassesDatabasdEntities Context;
        protected Repository()
        {
            Context = new ClassesDatabasdEntities ();
        }
 public void Commit()
        {
            Context.SaveChanges();
        }
        public void Add(T entity)
        {
            Context.CreateObjectSet<T>().AddObject(entity);
        }
        public void Update(T entity)
        {
            Context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            Context.SaveChanges();
        }
        public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }
        public void Dispose()
        {
            if (Context != null)
            {
                Context.Dispose();
            }
            GC.SuppressFinalize(this);
        }
}
公共抽象类存储库:IRepository,IDisposable其中T:class
{
私有类数据库身份上下文;
受保护的存储库()
{
Context=newclassesdatabasedentities();
}
公共无效提交()
{
SaveChanges();
}
公共无效添加(T实体)
{
Context.CreateObjectSet().AddObject(实体);
}
公共无效更新(T实体)
{
Context.ObjectStateManager.ChangeObjectState(entity、System.Data.EntityState.Modified);
SaveChanges();
}
公共作废删除(T实体)
{
删除对象(实体);
SaveChanges();
}
公共空间处置()
{
if(上下文!=null)
{
Context.Dispose();
}
总干事(本);
}
}
我创建了类别dataaccessmanager,如

public class CategoryDataAccessManager : Repository<Model.Category>, ICategoryDataAccessManager
    {
        public bool SaveCategory(Model.Category category)
        {
            try
            {
                if (this.Get(t => t.Id == category.Id).Any())
                {
                    this.Update(category);
                }
                else
                {
                    this.Add(category);

                }

                return true;
            }
            catch (Exception)
            {

                return false;
            }


        }
公共类CategoryDataAccessManager:存储库,ICategoryDataAccessManager
{
公共bool SaveCategory(Model.Category)
{
尝试
{
if(this.Get(t=>t.Id==category.Id).Any())
{
更新(类别);
}
其他的
{
增加(类别);
}
返回true;
}
捕获(例外)
{
返回false;
}
}
它调用
this.Add(category);
但是如果不使用insert,您知道如何解决这个问题,问题是新类别的Id带有Id=0

SaveChanges()
需要在
Add()
中调用

public void Add(T entity)
{
    Context.CreateObjectSet<T>().AddObject(entity);
    Context.SaveChanges();
}
公共作废添加(T实体)
{
Context.CreateObjectSet().AddObject(实体);
SaveChanges();
}

或者
this.Commit()
应该在
this.Add(category)
之后调用
I面临更新和删除问题:ObjectStateManager不包含引用“ArabicEWorld.Model.category”类型对象的ObjectStateEntry。知道如何解决这个问题吗