使用Dotnet.core

时间:2017-04-18 20:23:05

标签: c# visual-studio-2015 dependency-injection entity-framework-core

我的sqlserver中有一个名为Fruit的数据库。我想创建一个C#控制台应用程序(.net核心)来访问数据,并在用户输入数据时将其保存。 我有一个名为Fruits的类和一个名为FruitDbContext的dbContext,它存储dbSet 如何创建依赖注入,以便我可以轻松地将模型轻松保存到不同的数据库?目前,我只想关注Microsoft SQL Server,而不用担心其他数据库。

我的水果课:

using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp1.Entity
{
    public class Fruit
    {
        public string ID { get; set; }
        public string FruitName { get; set; }
        public string FruitColor { get; set; }
    }
}

我的FruitDbContext类

namespace ConsoleApp1.Entity
{
    public class FruitDbContext : DbContext
    {
        public DbSet<Fruit> Fruits { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder)
        {
            optionBuilder.UseSqlServer(@"Server = xxx; Database=Test; Integrated Security = True");
        }
    }
}

我的主要计划:我的目的只是用一些种子记录启动和填充我现有的数据库

using ConsoleApp1.Entity;
using System;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var db = new FruitDbContext())
            {
                db.Fruits.AddRange(new Fruit { FruitName = "Orange", FruitColor = "Green" },
                    new Fruit { FruitName = "Banana", FruitColor = "Yellow" });
                var count=db.SaveChanges();
                Console.WriteLine($"{count} records added");
                foreach (var f in db.Fruits)
                {
                    Console.WriteLine($"Name -{f.FruitName}\t\t Color - {f.FruitColor}");
                }
            }
            Console.ReadLine();
        }
    }
}

当我尝试saveChanges时:我遇到了以下错误:

System.Data.SqlClient.SqlException: Invalid object name 'Fruits'.

我可能理解抱怨的内容但我不知道如何正确修复它,因为我是entityframeworkcore的新手。如果你已经对实体框架做了很多工作并且可以给我一些提示,那将非常感激。

这是我的project.json

{
    "buildOptions": {
        "emitEntryPoint": true
    },
    "dependencies": {
        "Microsoft.EntityFrameworkCore.Design": "1.1.1",
        "Microsoft.EntityFrameworkCore.SqlServer": "1.1.1",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.1",
        "Microsoft.EntityFrameworkCore.Tools": "1.1.0",
        "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.1"
        }
    },
    "frameworks": {
        "netcoreapp1.0": {
            "imports": "dnxcore50"
        }
    },
    "tools": {
        "Microsoft.EntityFrameworkCore.Tools": "1.1.0",
        "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final"
    },
    "version": "1.0.0-*"
}

我的项目结构很简单:

enter image description here

我需要做些什么才能解决问题? 的更新

我在SQL中的表

USE [Test]
GO

/****** Object:  Table [dbo].[Fruit]    Script Date: 4/16/2017 10:53:45 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Fruit](
    [ID] [uniqueidentifier] NOT NULL,
    [FruitName] [nvarchar](50) NULL,
    [FruitColor] [nvarchar](50) NULL,
 CONSTRAINT [PK_Fruit] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:0)

确保您的媒体资源名称public DbSet<Fruit> Fruits { get; set; }与您的表名相匹配。因此,该表也应该被称为Fruits,或者您应该更改属性名称以匹配表名。

相关问题