0
코드 우선 모드에서 Entity Framework를 사용하여 일부 개체를 저장하려고하는데 키 생성에 문제가 있거나 기존 개체를 변경하고 싶지 않습니다. 키 삽입). 다음은 내가 겪고있는 문제의 예입니다.엔터티 프레임 워크 키 목록
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class AClass
{
[Key]
public int id { get; set; }
public List<SubClass> l;
};
public class SubClass
{
public string a;
public string b;
};
public class ADbContext : DbContext
{
public DbSet<AClass> dbset { get; set; }
}
class Program
{
static void Main(string[] args)
{
ADbContext db = new ADbContext();
AClass a = new AClass();
a.id = 1;
a.l = new List<SubClass>();
SubClass sc=new SubClass();
sc.a = "Hello";
sc.b = "World";
a.l.Add(sc);
SubClass sca = new SubClass();
sca.a = "Stack";
sca.b = "Overflow";
a.l.Add(sca);
db.dbset.Add(a);
db.SaveChanges();
}
}
}
이 프로그램을 실행하면 개체 하위 클래스에 기본 키가 없다는 오류가 발생합니다.
AClass:
id int public key
SubClass:
AClass_id int foreign key references AClass(id),
postition_in_list_id int,
a string,
b string,
primary key (AClass_id,position_in_list_id)
내가 ID 1 내가 좋겠와 AClass 말할 것이다 : 나는 데이터베이스에서 이러한 개체를 저장하는 엔티티 프레임 워크를 사용하지 않고 손으로 프로그램을 작성하는 경우 이제, 내가 무엇을 할 것은 같은 테이블이 있습니다 내가 의미하는 바를 본다면 SubClass (1,1), (1,2), (1,3)을가집니다.
Entity Framework에서이 작업을 수행 할 수있는 방법이 있습니까? 아니면 임의의 ID를 SubClass에 추가해야합니까?
하위 클래스는 AClass의 일부로 제외하고는 데이터베이스에 저장되지 않습니다.
,이 처분 할'dbcontext'와'using' 블록을 사용합니다. –