c#


EF Fluent API + two column composite uniqueness + autoincrement primary key


I use Fluent API. I don't like annotations.
I like to always use an autoincrement as my primary key, in all of my tables.
Some of my tables require that two columns, X and Y (where X is not the autoincrement key and Y is not the autoincrement key) have to be unique, ie: there can't be another row such that it has X1=X2 and Y1=Y2. If I wasn't using an autoincrement key, I would simply make these two the key, like this:
modelBuilder.Entity<Foo>()
.HasKey(t => new { t.X, t.Y })
.ToTable("Foos");
But, as I said in (2), I'm using autoincrement primary keys
modelBuilder.Entity<Foo>()
.HasKey(t => t.someLongId)
.ToTable("Foos");
How can I achieve this composite uniqueness in Fluent API?
This is what I want to achieve, written in SQL:
CREATE TABLE `Foos` (
`ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
...
PRIMARY KEY (`ID`),
UNIQUE KEY (`X`, `Y`)
);
You can achieve this using the 'HasColumnAnnotation(...)' method and applying an IndexAnnotation > IndexAttribute.
modelBuilder.Entity<Foo>()
.Property(t => t.X)
.HasColumnAnnotation("X", new IndexAnnotation(new IndexAttribute("X") { IsUnique = true }));
You can find further information here (MSDN)
Aydin's answer had the concept (IndexAnnotation and HasColumnAnnotation) but it wasn't involving the other columns. Here is a complete answer that worked for me:
modelBuilder
.Entity<Foo>()
.Property(t => t.X)
.IsRequired()
.HasMaxLength(60)
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_X_Y", 1) { IsUnique = true }));
modelBuilder
.Entity<Foo>()
.Property(t => t.Y)
.IsRequired()
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_X_Y", 2) { IsUnique = true }));
That, supposing that X is string column and Y is not (just to show how .HasMaxLength(60) can be used in the string column)
I will accept Aydin's answer though.

Related Links

Parse exception on SaveAsync()
C# Make Form1 Do An Action After Form2 is Closed
ASP.NET - How can I check all checkboxes across gridview pages at once and access checked rows in a gridview across pages?
Trying to build a procedure to create a variable but it gets trapped inside
Write to file with FileStream without using “using”
Unable to run Enable-Migration VS2013 SP4
ViewBag is passing changed value PartialView
Serializing a field with protobuf results in a 0 bytes data file
BDD Specflow - Scenario Outline where text spans multiple lines
Shared SQL Database on UNC Share with Entity Framework
CodeFirst mapping relationship between entities
Create user friendly public/private key pairs
How to cache the image of the control?
ValidationMessageFor automatically getting fred
Generic Type instance with anonymous type as generic argument, how to convert back to original type properly?
Passing an object to constructor when using StructureMap 3

Categories

HOME
asterisk
model-view-controller
kendo-ui-angular2
grizzly
ns2
oracle-sqldeveloper
qemu
packet
mysqli
aix
bigtable
reference
data-modeling
ms-access-2010
vsixmanifest
subprocess
urllib2
watson-dialog
yui
background-image
tag-cloud
alert
indri
draw2d
django-rest-auth
connectiq
quality-center
uipath
android-xml
rollupjs
workflow-foundation
large-files
windows-10-mobile
mobile-development
openoffice-impress
edsdk
lines
spring-oauth2
unity3d-5
ponylang
rtsp
pygraphviz
latency
web-technologies
rexx
android-doze-and-standby
anonymous-function
yank
playframework-2.3
jenkins-docker
fckeditor
totalview
multi-select
acceleo
scikit-image
settext
mrunit
angular-i18n
bbedit
computability
controlpanel
updating
tokudb
opennebula
asp.net-2.0
jsonmodel
powershell-v1.0
nsmutableurlrequest
disparity-mapping
disabled-control
mvcgrid
episerver-7
android-relativelayout
new-operator
mongoid4
nomachine
xml-validation
pechkin
wordbreaker
simplemembership
string-length
mmo
diazo
windows-xp-embedded
mismatch
cryptolicensing
netbeans7.0
arraycollection
mdd
coderush-xpress
ffdshow
bootstrap-protocol
paperless

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile