2014-09-05 5 views
4

그래서 나는이 코드 첫 번째 클래스가 열에 NULL 값을 삽입 할 수 없습니다EF 코드 먼저

IList<Session> defaultSessions = new List<Session>(); 
defaultSessions.Add(new Session() 
{ 
    Start = DateTime.Parse("09/01/2014 14:00:00"), 
    End = DateTime.Parse("09/01/2014 14:10:00"), 
    Type = "pharyngeal", 
    PatientID = 1, 
}); 
foreach (Session session in defaultSessions) 
{ 
    context.Sessions.Add(session); 
} 

SessionID가 기본 키이고 Auto-Incremented라고 가정하기 때문에 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]을 올바르게 사용하는 이유는 무엇입니까?

잘 여전히이 오류를 얻을 :

Cannot insert the value NULL into column table column does not allow nulls. INSERT fails. The statement has been terminated

데이터베이스의 테이블이 (내가 LocalDB를 사용하고 있습니다, 데이터베이스 인스턴스 mssqllocaldb 아래처럼 보이는

:
enter image description here
무엇 오전 내가 일을 잘못

편집 : 나는 데이터베이스에, 신원이 FALSE이라고 지적 이 스크린 샷 참조 : enter image description here
최신 이전에, 내가 정체성에 대한 수정을했기 때문에 이것은 나를 이상한 파업은 (내가이 질문에 제공하는 단지 클래스의), 및 파일은 다음과 같습니다

public partial class IdentityFixes : DbMigration 
{ 
    public override void Up() 
    { 
     DropPrimaryKey("dbo.Admin"); 
     DropPrimaryKey("dbo.Session"); 
     AlterColumn("dbo.Admin", "AdminID", c => c.Int(nullable: false, identity: true)); 
     AlterColumn("dbo.Session", "SessionID", c => c.Int(nullable: false, identity: true)); 
     AddPrimaryKey("dbo.Admin", "AdminID"); 
     AddPrimaryKey("dbo.Session", "SessionID"); 
    } 

    public override void Down() 
    { 
     DropPrimaryKey("dbo.Session"); 
     DropPrimaryKey("dbo.Admin"); 
     AlterColumn("dbo.Session", "SessionID", c => c.Int(nullable: false)); 
     AlterColumn("dbo.Admin", "AdminID", c => c.Int(nullable: false)); 
     AddPrimaryKey("dbo.Session", "SessionID"); 
     AddPrimaryKey("dbo.Admin", "AdminID"); 
    } 
} 

로 당신은 확인할 수 있습니다, 정체성은 수정에서 사실로 설정됩니다. 나는 Update-Database을 수행했는데 왜 이것이 작동하지 않는지 이해하지 못합니다.

+3

는 실제로 전송되는 내용 쿼리를 확인하기 위해 데이터베이스에 프로파일 러를 실행 봤어? – MattC

+0

@MattC 아니요. 당신이 무슨 말을하고 어떻게하는지 모르기 때문에 제가하지 않았습니다. Entity Framework에 관한 모든 설명서에 따르면이 기능을 사용해야합니까? – QuantumHive

+0

EFProfiler 데모 (EF로 작업 할 때 중요한 도구)를 응용 프로그램에 통합하고 (Main의 한 줄) SQL에 전송되는 명령문을 볼 수 있습니다. 구성이 잘되었으므로 디버깅 할 수있는 유일한 방법은 EF가 데이터베이스로 보내는 내용을 보는 것입니다. 'new Session()'을'context.Sessions.Create()'로 바꾸어보십시오. –

답변

0

기존 데이터베이스에 마이그레이션을 적용하여 비슷한 문제가 발생했기 때문에 빈 마이그레이션을 만들고 SQL 코드를 사용했습니다. 여기에 제 예제가 있습니다.

public override void Up() 
{ 
    DropForeignKey("dbo.Bar", "FooId", "dbo.Foo"); 
    Sql("IF object_id(N'[dbo].[FK_Bar_Foo]', N'F') IS NOT NULL ALTER TABLE [dbo].[Bar] DROP CONSTRAINT [FK_Bar_Foo]"); 
    Sql("CREATE TABLE [dbo].[FooTemp] ([Id] int NOT NULL)"); 
    Sql("INSERT INTO [dbo].[FooTemp] ([Id]) SELECT [Id] FROM [dbo].[Foo]"); 
    Sql("DROP TABLE [dbo].[Foo]"); 
    Sql("CREATE TABLE [dbo].[Foo] ([Id] int IDENTITY (1,1) NOT NULL)"); 
    Sql("SET IDENTITY_INSERT [dbo].[Foo] ON"); 
    Sql("INSERT INTO [dbo].[Foo] ([Id]) SELECT [Id] FROM [dbo].[FooTemp]"); 
    Sql("SET IDENTITY_INSERT [dbo].[Foo] OFF"); 
    Sql("DROP TABLE [dbo].[FooTemp]"); 
    AddPrimaryKey("dbo.Foo", "Id"); 
    AddForeignKey("dbo.Bar", "FooId", "dbo.Foo", "Id"); 
} 

public override void Down() 
{ 
    DropForeignKey("dbo.Bar", "FooId", "dbo.Foo"); 
    Sql("CREATE TABLE [dbo].[FooTemp] ([Id] int NOT NULL)"); 
    Sql("INSERT INTO [dbo].[FooTemp] ([Id]) SELECT [Id] FROM [dbo].[Foo]"); 
    Sql("DROP TABLE [dbo].[Foo]"); 
    Sql("CREATE TABLE [dbo].[Foo] ([Id] int NOT NULL)"); 
    Sql("INSERT INTO [dbo].[Foo] ([Id]) SELECT [Id] FROM [dbo].[FooTemp]"); 
    Sql("DROP TABLE [dbo].[FooTemp]"); 
    AddPrimaryKey("dbo.Foo", "Id"); 
    AddForeignKey("dbo.Bar", "FooId", "dbo.Foo", "Id"); 
} 

HTH는