Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用EF DbContext保存更改_C#_Entity Framework_Entity Framework 4.1 - Fatal编程技术网

C# 无法使用EF DbContext保存更改

C# 无法使用EF DbContext保存更改,c#,entity-framework,entity-framework-4.1,C#,Entity Framework,Entity Framework 4.1,我从“编程实体框架代码优先”开始学习EF代码。以下代码段从第5页复制到第7页 访问 动物型 病人 使用系统; 使用System.Collections.Generic; 名称空间第一章 { 班级病人 { 公共病人() { 访问量=新列表(); } 公共int Id{get;set;} 公共字符串名称{get;set;} 公共日期时间出生日期{get;set;} 公共AnimalType AnimalType{get;set;} 公共日期时间首次访问{get;set;} 公共列表访问{get;se

我从“编程实体框架代码优先”开始学习EF代码。以下代码段从第5页复制到第7页

访问 动物型 病人
使用系统;
使用System.Collections.Generic;
名称空间第一章
{
班级病人
{
公共病人()
{
访问量=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共日期时间出生日期{get;set;}
公共AnimalType AnimalType{get;set;}
公共日期时间首次访问{get;set;}
公共列表访问{get;set;}
}
}
VetContext.cs
使用System.Data.Entity;
名称空间第一章
{
类VetContext:DbContext
{
公共数据库集患者{get;set;}
公共数据库集访问{get;set;}
}
}
Program.cs
使用系统;
使用System.Collections.Generic;
名称空间第一章
{
班级计划
{
静态void Main(字符串[]参数)
{
var dog=新的动物类型{TypeName=“dog”};
var患者=新患者
{
Name=“辛普森”,
生日=新的日期时间(2008年1月28日),
动物类型=狗,
访问量=新列表
{ 
新访问
{ 
日期=新的日期时间(2011年9月1日)
}
}
};
使用(var context=new VetContext())
{
context.Patients.Add(患者);
SaveChanges();
}
}
}
}
不幸的是,我得到了以下错误。你能告诉我怎么了吗


不确定这是否是您的确切错误的原因,但也可能导致另一个错误;您的
VetContext
应再包含一行:

public DbSet<AnimalType> AnimalTypes { get; set; }

记录到。

可能您没有填写所有必填字段。我注意到的是
患者。sql server不接受FirstVisite
默认值。

此消息告诉我们“出现了问题。如果您想知道具体情况,请查看
内部异常。如果单击异常消息中的“查看详细信息”链接,则可以执行此操作。如果没有这些信息,这里没有人能够提供帮助。我对SqlServer没有太多经验,但我认为如果未指定,它将插入
DateTime.MinValue
。SqlServer不会发生这种情况吗?
namespace ChapterOne
{
    class AnimalType
    {
        public int Id { get; set; }
        public string TypeName { get; set; }
    }
}
using System;
using System.Collections.Generic;

namespace ChapterOne
{
    class Patient
    {
        public Patient()
        {
            Visits = new List<Visit>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime BirthDate { get; set; }
        public AnimalType AnimalType { get; set; }
        public DateTime FirstVisit { get; set; }
        public List<Visit> Visits { get; set; }
    }
}
using System.Data.Entity;

namespace ChapterOne
{
    class VetContext : DbContext
    {
        public DbSet<Patient> Patients { get; set; }
        public DbSet<Visit> Visits { get; set; }
    }
}
using System;
using System.Collections.Generic;

namespace ChapterOne
{
    class Program
    {
        static void Main(string[] args)
        {
            var dog = new AnimalType { TypeName = "Dog" };

            var patient = new Patient
            {
                Name = "Simpson",
                BirthDate = new DateTime(2008, 1, 28),
                AnimalType = dog,
                Visits = new List<Visit> 
                { 
                    new Visit 
                    { 
                        Date = new DateTime(2011, 9, 1)
                    }
                }

            };

            using (var context = new VetContext())
            {
                context.Patients.Add(patient);
                context.SaveChanges();
            }
        }
    }
}
public DbSet<AnimalType> AnimalTypes { get; set; }
var dog = new AnimalType { TypeName = "Dog" };