简体   繁体   English

与 ef 对话和可定制的 asp.net 类库

[英]asp.net class library that talks to ef and customizable

I'm trying to make a class library that contains only the core logic of may app.我正在尝试制作一个仅包含 may app 核心逻辑的类库。 I've seen many tutorials that do this but they include the EF core inside that library.我见过很多这样做的教程,但它们在该库中包含了 EF 核心。 What I want to do is put only all the logic such, adding a category if it doesn't exist yet by passing a string .我想要做的是只放置所有逻辑,如果它还不存在,则通过传递string添加一个类别。

Here 's what I want to do这是我想要做的

  1. Create a class library in separated project.在分离的项目中创建一个类库。
  2. Add a class called SomeName Manager - [contains all the methods I want]添加一个名为SomeName Manager 的类 - [包含我想要的所有方法]
  3. Create models to be modified by this manager but I don't want this to be the class directly used as my entity.创建要由该manager修改的模型,但我不希望这成为直接用作我的实体的类。 I just want this to be the base class of my entity for customization.我只是希望它成为我的实体的基类以进行自定义。 for example if I have to add a new propery, I'd just change the entity in my main app.例如,如果我必须添加一个新属性,我只需更改主应用程序中的实体。 not in the library.不在图书馆。
  4. The DbContext is in my main app only. DbContext 仅在我的主应用程序中。 Which means all of my classes and lists that used in my library will be just in the memory.这意味着我在库中使用的所有类和列表都将仅存在于内存中。

Here's what I got so far这是我到目前为止所得到的

// class library
public interface IBook{
    // some properties here
    ICollection<ICategory> Categories { get; set; }
    // some more properties...
}
public interface ICategory{
    // some properties here
    ICollection<IBook> Books { get; set; }
    // some more properties...
}

public class Book : IBook {
    // implementations...
}
public class Category : ICategory {
    // implementations
}

public class BookManager {
    public void CreateBook(Book book) {
        // some logic
        // I'm not sure if I would pass Book or IBook
    }
    public void AddCategories(List<Category> categories) {
        // some logic
        // I'm not sure if I would pass Category or ICategory
    }
}

// my main app
public class BookInfo {
    // some props...
}
public class BookCategory {
    // some props...
}

public class MyDB : DbContext{
    public DbSet<BookInfo> BookInformations {get; set;}
    public DbSet<BookCategory> BookCategories {get; set;}
}

The problems问题

  1. The table name in my database is Book .我的数据库中的表名是Book I don't want to use fluent API just to rename this.我不想使用 fluent API 来重命名它。
  2. I have new column it both database called Discriminator .我有一个名为Discriminator数据库的新列。 What is that?那是什么? I do I remove that?我删除那个?
  3. Categories isn't binding to Book.Categories.类别对 Book.Categories 没有约束力。 It's empty but it inserts to the database.它是空的,但它插入到数据库中。

Additional Question Am I doing this right?附加问题我这样做对吗? Is it a bad idea?这是一个坏主意吗? Please enlighten me.请赐教。 I'm not beginner but I haven't been in the real development team.我不是初学者,但我没有加入真正的开发团队。

1 .The table name in my database is Book. 1 .我数据库中的表名是Book。 I don't want to use fluent API just to rename this.我不想使用 fluent API 来重命名它。

To give a class a different table name you can do this with the fluent API but since you don't want to do that you can use the [Table] attribute for this:要为类指定不同的表名,您可以使用 fluent API 执行此操作,但由于您不想这样做,您可以为此使用[Table]属性

[Table("BookTableName")]
public class Book : IBook {
    // implementations...
}

2. I have new column it both database called Discriminator. 2. 我有一个名为 Discriminator 的数据库的新列。 What is that?那是什么? I do I remove that?我删除那个?

A discriminator column is used to identify which type each row represents.鉴别器列用于识别每行代表的类型。 See the docs 查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM