【问题标题】:Database error while working with Entity Framework使用实体框架时出现数据库错误
【发布时间】:2018-04-24 14:03:53
【问题描述】:

我想要实现的目标非常简单。我只想拥有一个本地数据库文件,我可以通过使用实体框架在列表视图中显示记录。问题是,每次构建它时,我都会收到此错误:

“DiaryEntities”不包含“Title”的定义,并且找不到接受“DiaryEntities”类型的第一个参数的扩展方法“Title”(您是否缺少 using 指令或程序集引用?)

我的数据库名为Diary,我只有一个名为Entries 的表,其中包含TitleDateContent 列。这是我的文件:

//main.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace diary
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (diary.DiaryEntities de = new DiaryEntities())
            {
                var diaries = (from m in de.Diary select m.Title);
                listBox1.Items.AddRange(diaries.ToArray());
            }
        }
    }
}

//DiaryEntities.Context.cs
namespace jurnal1
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class DiaryEntities : DbContext
    {
        public DiaryEntities() : base("name=DiaryEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<DiaryEntities>Diary { get; set; }
    }
}

【问题讨论】:

  • 显示Entries的模型,并更改: public virtual DbSetDiary { get;放; } to public DbSetDiary { get;放; }
  • 您的DbSet&lt;&gt; 应该将实体类型作为通用参数,而不是上下文本身的类型 (DbSet&lt;Diary&gt;?)
  • @haim770 如果我将 Diary 放在 DbSet 中,那么我会得到“找不到类型或命名空间 Diary”
  • @hanoc salinas 我得到了相同的输出
  • 您需要一个与数据库中的表具有相同属性的实体(类),然后在 dbSet 上您需要更改我最后的评论方式

标签: c# database visual-studio entity-framework


【解决方案1】:

将您的代码更改为:

namespace jurnal1 {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
        using System.ComponentModel.DataAnnotations.Schema;

        public partial class DiaryEntities : DbContext {

            public DiaryEntities ( )
                : base ( "name=DiaryEntities" ) {
            }

            protected override void OnModelCreating ( DbModelBuilder modelBuilder ) {

            }
            public DbSet<Entries> Diary { get; set; }

        }

        [Table("entries")]
        public class Entries {
            public int Id { get; set; }
            public string Title { get; set; }
            public DateTime Date { get; set; }
            public string Content { get; set; }
        }
    }
}

check the GITHUB project我试试这个,我认为问题是:模型需要一个Key,所以,添加一个id,我认为你的数据库表有更多列,请尝试链接上的项目。

【讨论】:

  • 这似乎有效。但是当我按下按钮加载数据时,我得到一个未处理的异常,说:“实体类型 Entries 不是当前上下文模型的一部分。”
  • 链接怎么失效了?我现在更新了问题
  • 我无法打开 pastebin.com,此链接显示我无法解析 dns 或 ip
  • @DanielCîrstea 请看,答案已更新,我添加了指向 github 的链接
猜你喜欢
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 2015-03-23
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
相关资源
最近更新 更多