简体   繁体   English

实体框架的从属增量主键

[英]Dependant increment primary key with Entity Framework

I'm working with C#, Entity Framework 6.1.3 code-first, SQL Server for the first time, and I'm having problems creating a dependent autoincrementing key. 我第一次使用C#,Entity Framework 6.1.3代码优先,SQL Server,并且在创建依赖的自动增量键时遇到问题。

I have this: 我有这个:

class One
{
        [Key]
        [Required]
        string exampleKey { get; set; }
        string otherProperty { get; set; }
}

class Two
{
        [Required]
        [Key]
        [Column(Order = 0 )]
        public string exampleKey { get; set; }
        [ForeignKey("exampleKey")]
        public virtual One one { get; set; }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column(Order = 1)]
        [Required]
        public long Version { get; set; }

        public string otherProperty { get; set; }        
}

I'm interested in the Version property of class Two when you insert data it make ids like 我对类TwoVersion属性感兴趣,当您插入数据时,它会使id像

Class One One

|exampleKey|otherProperty|
|"test"    |"random data"|
|"test2"   |"more random"|

Class Two Two

|exampleKey|Version|otherProperty|
|"test"    |1      |"random data"|
|"test"    |2      |"more random"|
|"test2"   |3      |"random data"|
|"test2"   |4      |"more random"|

But I'm looking for something like this 但是我正在寻找这样的东西

|exampleKey|Version|otherProperty|
|"test"    |1      |"random data"|
|"test"    |2      |"more random"|
|"test2"   |1      |"random data"|
|"test2"   |2      |"more random"|

I am looking for a solution to this problem a long time ago, is it possible? 很久以前,我正在寻找解决此问题的方法,这可能吗?

Thanks so, so much! 非常感谢!

Not easily, no. 不容易,不。 But you can derive which is the most recent by a datestamp or an identity column. 但是,您可以通过日期戳或标识列来得出最新的。 Then whenever you retrieve your data, simply get the row with the most recent date/identity value. 然后,每当您检索数据时,只需获取具有最新日期/标识值的行即可。

You could also write a view which surfaces the behavior I just mentioned. 您也可以编写一个视图,以显示我刚才提到的行为。

Something like this: 像这样:

Fake Data 伪数据

if object_id('dbo.Data') is not null drop table dbo.Data
create table dbo.Data
(
    RID int identity(1,1) primary key clustered,
    ExampleKey varchar(10),
    OtherProperty varchar(100)
)

-- initial insert
insert into dbo.Data (ExampleKey, OtherProperty)
values ('test', 'Random data'), ('test2', 'more random')

-- Second insert
insert into dbo.Data (ExampleKey, OtherProperty)
values ('test', 'Random data'), ('test2', 'more random')

View Approach 查看方法

if object_id('dbo.vData') is not null drop view dbo.vData
go

create view dbo.vData
as

select 
    Version = row_number() over (partition by ExampleKey order by RID desc),
    ExampleKey,
    OtherProperty
from dbo.Data
go

select top 1000 *
from dbo.vData

Alternatives 备择方案

If you NEED to persist it in the table when it's inserted, You'll probably need a trigger (which I wouldn't recommend). 如果您需要在插入表时将其保留在表中,则可能需要一个触发器(我不建议这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM