기본 키의 일부로 UPC를 사용하는 제품 테이블이 있습니다. 제품에 UPC가 없을 때까지는 아무 문제가 없으며이를 해결하기위한 권장 방법은 8004 + ID 번호와 8005 + ID 번호 사이의 숫자를 생성하는 것입니다.Entity Framework 4 : ID 필드를 사용하지 않고 다음으로 높은 번호를 삽입하는 방법은 무엇입니까?
트랜잭션 중에 UPC가 0 인 경우 고유 한 UPC를 생성해야하며 그런 다음 UPC 값으로 0을 가진 제품에 대해서만 새 UPC를 검색 할 수 있어야합니다.
SQL, 나는이 작업을 수행 할 수 있습니다 :
insert into Product (ID, Name)
select min(pivotTable.value), 'New Product' as Name
from pivotTable
where not exists(
select null as nothing
from product
where pivotTable.value = product.ID) and
pivotTable.value > 8004000000 and pivotTable.value < 8005000000
select id
from product
where Name = 'New Product' -- assuming Name is unique
가 어떻게 이런 짓을 했을까 엔티티 프레임 워크 4에? 별도의 우려는 이것이 모두 단일 트랜잭션 아래에 있으므로 누락 된 UPC 세트를 지정하면 모든 UPC를 모든 새 제품에 할당 할 수 있습니다.
편집 :
나는이 다음으로 가장 높은 수를 얻을처럼 보이는 뷰를 작성 결국하지만 기본 키를 확인할 수 없기 때문에 EF는 다이어그램에서 테이블을 생성하지 않습니다. XML을 해킹하면 데이터베이스에서 업데이트 할 때까지 작동하므로 변경 사항이 지워집니다.
Select min(ID), 'New Product' as Name
from (select distinct ID
from product p1
where p1.ID > 8004000000 and p1.ID < 8005000000
union
select distinct coalesce(ID, 8004000000) as ID) A
left outer join
(select distinct ID
from product p2
where p2.ID > 8004000000 and p2.ID < 8005000000
union
select distinct coalesce(ID, 8004000000) as ID) B
on A.ID + 1 = B.ID
where B.ID is null
그래서 질문은 동일합니다
어떻게 당신이 엔티티에 Linq에에 위의 SQL 쿼리를 다시 작성할 수도 있고, 당신은 어떻게 얻을 수 있는지, 법인에서 적어도 가장 높은 숫자 즉, 프레임 워크 4를 생성 할 수 새로 고침시 변경 사항을 적용하는 XML 파일을 편집하지 않고 Entity Framework 4 다이어그램에 표시 할보기?
편집 :이 가능한 다음 사용 Linq에를 생성하는 것 같다
// Setup our ID list
var prod = DC.Products.Where(p => p.ID > 0 && p.ID < 1000)
.Select(p => p.ID).Distinct();
// Compare the list against itself, offset by 1. Look for "nulls"
// which represent "next highest number doesn't exist"
var q = (from p1 in prod
from p2 in prod.Where(a => a == p1 + 1).DefaultIfEmpty() // Left join
where p2 == 0 // zero is null in this case
select p1).Min();
var r = q + 1; // one higher than current didn't exist, so that's the answer