坚持我在这里做错了

时间:2018-08-23 14:44:10

标签: c# sql-server entity-framework linq

我在数据库中创建了3个表,并且为每个表创建了模型类。除了dbcontext派生的类和带有方法main的程序文件。当我尝试运行应用程序时,出现NullReferenceException。请提供有关我在做什么的建议。所有五个类文件都在下面。

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace Exo
{
    public class Products
    {
        [Key]
        public int ProductID { get; set; }

        [Required]
        [StringLength(50)]
        public string ProductName { get; set; }

        [Required]
        [Column("ProductCostPrice", TypeName ="money")]
        public decimal ProductCostPrice { get; set; }

        [Required]
        [Column("ProductSellingPrice", TypeName = "money")]
        public decimal ProductSellingPrice { get; set; }

        [Required]
        [Column("ProductUnitsInStock", TypeName = "smallint")]
        public int ProductUnitsInStock { get; set; }

        [Column(TypeName = "ntext")]
        public string ProductAttributes { get; set; }
    }
}

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Exo
{
    public class Expenses
    {
        [Key]
        public int ExpenseID { get; set; }

        [Required]
        public string ExpenseName { get; set; }

        [Required]
        public string ExpenseType { get; set; }

        [Required]
        [Column(TypeName = "money")]
        public decimal ExpenseAmount { get; set; }
    }
}

using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System;

namespace Exo
{
    public class Customers
    {
        [Key]
        public int CustomerID { get; set; }

        public string CustomerName { get; set; }
        public string CustomerEmail { get; set; }
        public string CustomerPhone { get; set; }
        public string CustomerLocation { get; set; }
        public string CustomerStatus { get; set; }

        [Required]
        public string ProductName { get; set; }
        public DateTime CustomerFUp { get; set; }

        [Column(TypeName ="ntext")]
        public string CustomerComments { get; set; }
    }
}

下面是dbcontext类和程序类

using Microsoft.EntityFrameworkCore;

namespace Exo
{
    public class Exo : DbContext
    {
        public DbSet<Customers> Customer { get; set; }
        public DbSet<Products> Product { get; set; }
        public DbSet<Expenses> Expense { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Data Source=(localdb)/mssqllocaldb;"+
                "Initial Catalog=Exo;"+
                "Integrated Security=true;"+
                "MultipleActiveResultSets=true;"
                );
        }

        /*protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customers>()
                .Property(customer => customer.CustomerName)
                .IsRequired()
                .HasMaxLength(50);
        }*/
    } 
}

using static System.Console;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Exo
{
    class Program
    {
        static void QueryingCustomers()
        {
            using (var db = new Exo())
            {
                WriteLine("Customers and some infomation: ");

                IQueryable<Customers> clients = db.Customer.Include(c => c.CustomerEmail);

                foreach(Customers c in clients)
                {
                    WriteLine($"{c.CustomerName} has email {c.CustomerEmail}");
                }
            }
        }

        static void Main(string[] args)
        {
            QueryingCustomers();
        }
    }
}

P.S:数据库中的表只有主键,并且已使用Visual Studio服务器浏览器中“显示表数据”选项中的数据手动填充。

0 个答案:

没有答案