c# – 存储库模式和一对多关系

c# – 存储库模式和一对多关系,第1张

概述我正在创建一个使用Entity Framework的应用程序.我有2个具有一对多关系的类.我决定使用一个设计模式Repository,据我所知这是一个很好的做法. 我的界面: using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;namespace Dat 我正在创建一个使用Entity Framework的应用程序.我有2个具有一对多关系的类.我决定使用一个设计模式Repository,据我所知这是一个很好的做法.
我的界面:

using System;using System.Collections.Generic;using System.linq;using System.linq.Expressions;namespace DataAccess.Repository{   public interface IRepository<T>   {       voID Insert(T entity);       voID Delete(T entity);       Iqueryable<T> SearchFor(Expression<Func<T,bool>> predicate);       IEnumerable<T> GetAll();       T GetByID(int ID);   }}

我的课

using System;using System.Collections.Generic;using System.Data.Entity;using System.linq;using System.linq.Expressions;using System.Text;using System.Threading.Tasks;using DataAccess.Repository;namespace DataAccess{    public class Repository<T> : IRepository<T>  where T : class    {        protected DbSet<T> DbSet;        public Repository(DbContext datacontext)        {            //DbContext.Set Method (Type)            //Returns a non-generic DbSet instance for access to entitIEs of the given type in the context and the underlying store.            DbSet = datacontext.Set<T>();        }        public voID Insert(T entity)        {            DbSet.Add(entity);        }        public voID Delete(T entity)        {            DbSet.Remove(entity);        }        public Iqueryable<T> SearchFor(Expression<Func<T,bool>> predicate)        {            return DbSet.Where(predicate);        }        public IEnumerable<T> GetAll()        {            return DbSet;        }        public T GetByID(int ID)        {            return DbSet.Find(ID);        }    }}

这是我的两个模型类

using System;using System.Collections.Generic;using System.Globalization;using System.linq;using System.Text;using System.Threading.Tasks;namespace Model{    public class Book    {        public int BookID { get; set; }        public string name { get; set; }        public string Author { get; set; }        public string Ganre { get; set; }        public int Size { get; set; }        public string Path { get; set; }    }}

using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace Model{    public  class category{        public category()        {            Books = new List<Book>();        }        public int categoryID { get; set; }        public string categoryname { get; set; }        virtual public ICollection<Book> Books { get; set; }        }}

但我的问题是,如何将这本书添加到一个类别?
这是我的实施示例,但书籍未添加到该类别中.但是,当我想要获得所有书籍或所有类别时,一切正常.

using System;using System.Collections;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;using System.Data.Entity;using Model;using DataAccess;namespace TestDB{    class Program    {        static voID Main(string[] args)        {            Database.Setinitializer(                new DropCreateDatabaseIfModelChanges<BookShelfContext>());            using (var db = new BookShelfContext())            {                var book = new Book                {                    Author = "Author name",Ganre = "Ganre",name = "Book name",Path = @"Path",Size = 10                };                var category = new category                {                    categoryname = "Interesting"                };                var bookrepository = new Repository<Book>(db);                var categoryrepository = new Repository<category>(db);                IEnumerable<Book> books = bookrepository.GetAll();                IEnumerable<category> categorIEs = categoryrepository.GetAll();                //get all books for example                foreach (var b in books)                {                    Console.Writeline(b.name);                }            }            Console.ReadKey();        }     } }

非常感谢您的帮助.有一个美好的一天,更少的错误)

解决方法 将上下文添加到存储库,以便您可以实现SaveChanges方法:

protected Readonly DbContext context;    public Repository(DbContext datacontext)    {        DbSet = datacontext.Set<T>();        context = datacontext;    }    public voID SaveChanges()    {        context.SaveChanges();    }

然后,为了将书籍添加到现有的Bookcategory,只需将该书添加到category的集合中并保存该类别:

var categoryrepository = new Repository<category>(db);var mycategory = categoryrepository.GetByID(1);mycategory.Books.Add(book);categoryrepository.SaveChanges();

请记住调用SaveChanges以便将数据保留在数据库中. EF足够聪明,可以注意到您在该类别中添加了一个子项,并将其标记为已添加.在保存更改时,它会将其与所需的外键一起插入数据库.

总结

以上是内存溢出为你收集整理的c# – 存储库模式和一对多关系全部内容,希望文章能够帮你解决c# – 存储库模式和一对多关系所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1231167.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存