加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-2 – EF CTP5错误:对象名称无效

发布时间:2020-12-16 07:36:03 所属栏目:asp.Net 来源:网络整理
导读:我在scottgu的博客上关于EF代码第一个CTP5的示例,但我得到了错误 System.Data.SqlClient.SqlException: Invalid object name ‘dbo.Products’. 这是我得到的代码. add name="CTP5Context" connectionString="data source=.SQLEXPRESS;Integrated Security=
我在scottgu的博客上关于EF代码第一个CTP5的示例,但我得到了错误

System.Data.SqlClient.SqlException:
Invalid object name ‘dbo.Products’.

这是我得到的代码.

<add name="CTP5Context"
     connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|EFCTP5.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />


public class CTP5Context : DbContext 
{
    public DbSet<Product> Products { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public int Amount { get; set; }
}



var context = new CTP5Context();
        var products = context.Products;            

        return View(products);

我有点无能为力我在博客中做了同样的事情,这不是我第一次使用EF(但是CTP5 tho),我是在忽视什么?

解决方法

如果您的表名是数据库中的Product,请尝试以下操作:

[Table("Product",SchemaName = "dbo")]
public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public int Amount { get; set; }
}

要使用Table属性,您需要添加以下using语句:

using System.ComponentModel.DataAnnotations;

希望这可以帮助!它对我有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读