简体   繁体   English

在实体框架中为单个表手动创建模型

[英]Manually create model for single table in Entity Framework

Q1. Q1。 How do I manually create a dead-simple entity framework model for a one-column table in my database, and query it? 如何为数据库中的一栏表格手动创建一个简单的实体框架模型并进行查询?

The table looks like this: 该表如下所示:

CREATE TABLE dbo.MyTable (
    Value int NOT NULL CONSTRAINT PK_MyTable PRIMARY KEY CLUSTERED
);

And I have a POCO to map to it: 我有一个POCO可以映射到它:

public class MyTable {
    public int Value { get; set; }
}

Q2. Q2。 Then, how do I query MyTable with an Expression<Func<MyTable, bool>> lambda that will decide which rows to return and will get projected into the SQL? 然后,如何使用Expression<Func<MyTable, bool>> lambda查询MyTable ,它决定要返回的行并将其投影到SQL中?

I am relatively new to EF, but not to C# or software development. 我对EF比较陌生,但对C#或软件开发却不熟悉。 I'm asking this question because right now I just want to do a quick proof of concept of something in LINQPad, without using the EF Entity Data Model Wizard so it's easy to whip out code like this in the future. 我之所以要问这个问题,是因为现在我只想快速证明LINQPad中的某些概念,而无需使用EF实体数据模型向导,因此将来很容易编写出这样的代码。

All you need is in the code below, ready to be pasted to LinqPad 您需要的只是下面的代码,可以粘贴到LinqPad

class MyTable
{
    public int Value { get; set; }
}

class MyTableConfiguration : EntityTypeConfiguration<MyTable>
{
    public MyTableConfiguration()
    {
        ToTable("dbo.MyTable");
        HasKey(x => x.Value);
        Property(x => x.Value).HasColumnName("Value").IsRequired();
    }
}

class MyDbContext : DbContext
{
    public IDbSet<MyTable> MyTableSet { get; set; }

    public MyDbContext(string connectionString) : base(connectionString)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new MyTableConfiguration());
    }
}

void Main()
{
    MyDbContext context = new MyDbContext("Data Source=(local);Initial Catalog=SO33426289;Integrated Security=True;");
    Expression<Func<MyTable, bool>> expr = x => x.Value == 42;
    context.MyTableSet.Where(expr).Dump();
}

You need to make sure to reference EntityFramework NuGet package and System.ComponentModel.Annotations.dll . 您需要确保引用EntityFramework NuGet包和System.ComponentModel.Annotations.dll Here are the namespaces that I used: 这是我使用的名称空间:

System.ComponentModel.DataAnnotations.Schema
System.Data.Entity
System.Data.Entity.ModelConfiguration
System.Data.Entity.ModelConfiguration.Configuration
  1. Either use EF's code first (eg data annonations) to define mappings and create a context class to access the entity set through or use an EDMX (model first or database first) for creating the models and the mappings and have the model and context generated for you. code first使用EF的code first (例如,数据注释)来定义映射,然后创建上下文类来访问实体集,或者使用EDMX (首先是模型,首先是数据库)来创建模型和映射,并为之生成模型和上下文。您。 Search for any getting started with Entity Framework guide online. 在线搜索有关Entity Framework指南的任何入门。

Eg Code First (Search for Create the Data Model in this page) or for EDMX (Search for Creating the Entity Framework Data Model in this page). 例如代码优先 (在此页面中搜索Create the Data Model )或EDMX (在此页面中搜索Creating the Entity Framework Data Model )。

  1. Like so: 像这样:

using (var context = new YourContext()) { var filteredEntities = context.YourEntities.Where(expression).ToList(); }

However, your table will need a PK (primary key) for this to work. 但是,您的表将需要一个PK(主键)才能起作用。

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

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