<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl-pl">
<link rel="self" type="application/atom+xml" href="https://forum.atnel.pl/feed.php?f=59&amp;t=16631&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2016-10-27T11:40:54+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=59&amp;t=16631&amp;mode</id>
<entry>
<author><name><![CDATA[perlon]]></name></author>
<updated>2016-10-27T11:40:54+01:00</updated>
<published>2016-10-27T11:40:54+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172971#p172971</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172971#p172971"/>
<title type="html"><![CDATA[Re: C# Entity Framework nazwa klucza obcego]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172971#p172971"><![CDATA[
Zmagań ciąg dalszy. Wymyśliłem sobie, że w DictionarySlave para DictionaryMaster i DictSlaveKey będą stanowiły indeks unikalny. Czyli że w ramach klucza głównego klucze podrzędne muszą być unikalne natomiast klucze podrzędne mogą się powtarzać w różnych kluczach nadrzędnych i nie chodzi tu relację master to slave a o duble kluczy slave z różnymi DS_DMID.<br />Z atrybutami wygląda to tak:<br />[syntax=csharp]&#91;Table(&quot;dictionaryslave&quot;)&#93;<br />    public class DictionarySlave<br />    {<br />        &#91;Key&#93;<br />        &#91;Column(&quot;ID&quot;)&#93;<br />        public int DictionarySlaveID { get; set; }<br /> <br />        &#91;Column(&quot;DS_KEY&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;), StringLength(10)&#93;<br />        &#91;Index(&quot;IX_DmWithKey&quot;, 2, IsUnique = true)&#93;<br />        public string DictSlaveKey { get; set; }<br /> <br />        &#91;Column(&quot;DS_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictSlaveDescription { get; set; }<br /> <br />        &#91;Column(&quot;DS_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /> <br />        &#91;Column(&quot;DS_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /> <br />        &#91;ForeignKey(&quot;DictionaryMaster&quot;)&#93;<br />        &#91;Column(&quot;DS_DMID&quot;)&#93;<br />        &#91;Index(&quot;IX_DmWithKey&quot;, 1, IsUnique = true)&#93;<br />        public int DictionaryMasterID { get; set; }<br /> <br />        public DictionaryMaster DictionaryMaster { get; set; }<br />    }[/syntax]<br /><br />i daje to taką migrację :<br />[syntax=csharp]CreateTable(<br />                &quot;dbo.dictionaryslave&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DS_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DS_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DS_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DS_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                        DS_DMID = c.Int(nullable: false),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .ForeignKey(&quot;dbo.dictionarymaster&quot;, t =&gt; t.DS_DMID, cascadeDelete: true)<br />                .Index(t =&gt; new { t.DS_DMID, t.DS_KEY }, unique: true, name: &quot;IX_DmWithKey&quot;)<br />                .Index(t =&gt; t.DS_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DS_DESCRIPTION, name: &quot;IX_Description&quot;);[/syntax]<br /><br />Czy da się index IX_DmWithKey zrobić we FluentAPI bez tworzenia dodatkowych pól w klasie DictionarySlave? Od biedy mógłby być to klucz główny. Choć jak widać powyżej nie musi.<br />Oczywiście poniższe nie działa<br />[syntax=csharp]this.HasKey(e =&gt; new { e.DictionaryMaster, e.DictSlaveKey }); <br />The property 'DictionaryMaster' cannot be used as a key property on the entity 'sopdmodel.Models.DictionarySlave' because the property type is not a valid key type. Only scalar types, string and byte&#91;&#93; are supported key types.<br /> <br />lub <br /> <br />this.HasKey(e =&gt; new { e.DictionaryMaster.Id, e.DictSlaveKey });<br />The properties expression 'e =&gt; new &lt;&gt;f__AnonymousTypea`2(Id = e.DictionaryMaster.Id, DictSlaveKey = e.DictSlaveKey)' is not valid. The expression should represent a property: C#: 't =&gt; t.MyProperty'  VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't =&gt; new { t.MyProperty1, t.MyProperty2 }'  VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=382">perlon</a> — 27 paź 2016, o 11:40</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[perlon]]></name></author>
<updated>2016-10-27T09:36:18+01:00</updated>
<published>2016-10-27T09:36:18+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172957#p172957</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172957#p172957"/>
<title type="html"><![CDATA[Re: C# Entity Framework nazwa klucza obcego]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172957#p172957"><![CDATA[
Wczoraj było już chyba za późno na myślenie. Ostateczna wersja która spełnia wymagania (he, he sam sobie odpowiadam, ale może przyda się to innym czytającym):<br />[syntax=csharp]namespace sopdmodel.Models<br />{<br />    using RichardLawley.EF.AttributeConfig;<br />    using System.Collections.Generic;<br />    using System.ComponentModel.DataAnnotations;<br />    using System.ComponentModel.DataAnnotations.Schema;<br /> <br />    &#91;Table(&quot;dictionarymaster&quot;)&#93;<br />    public class DictionaryMaster<br />    {<br />        &#91;Key&#93;<br />        &#91;Column(&quot;ID&quot;)&#93;<br />        public int Id { get; set; }<br /> <br />        &#91;Column(&quot;DM_KEY&quot;, TypeName=&quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;, IsUnique = true),StringLength(10)&#93;<br />        public string DictMasterKey { get; set; }<br /> <br />        &#91;Column(&quot;DM_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictMasterDescription { get; set; }<br /> <br />        &#91;Column(&quot;DM_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue1 { get; set; }<br /> <br />        &#91;Column(&quot;DM_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue2 { get; set; }<br /> <br />        public ICollection&lt;DictionarySlave&gt; DictionarySlaves { get; set; }<br /> <br />    }<br /> <br />    &#91;Table(&quot;dictionaryslave&quot;)&#93;<br />    public class DictionarySlave<br />    {<br />        &#91;Key&#93;<br />        &#91;Column(&quot;ID&quot;)&#93;<br />        public int Id { get; set; }<br /> <br />        &#91;Column(&quot;DS_KEY&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;), StringLength(10)&#93;<br />        public string DictSlaveKey { get; set; }<br /> <br />        &#91;Column(&quot;DS_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictSlaveDescription { get; set; }<br /> <br />        &#91;Column(&quot;DS_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /> <br />        &#91;Column(&quot;DS_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /> <br />        &#91;ForeignKey(&quot;DictionaryMaster&quot;)&#93;<br />        &#91;Column(&quot;DS_DMID&quot;)&#93;<br />        public int DictionaryMasterID { get; set; }<br /> <br />        public DictionaryMaster DictionaryMaster { get; set; }<br />    }<br />}[/syntax]<br /><br />Daje to migrację dokłądnie taką o którą mi chodziło:<br /><br />[syntax=csharp]public partial class DictMapp : DbMigration<br />    {<br />        public override void Up()<br />        {<br />            CreateTable(<br />                &quot;dbo.dictionarymaster&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DM_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DM_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DM_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DM_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .Index(t =&gt; t.DM_KEY, unique: true, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DM_DESCRIPTION, name: &quot;IX_Description&quot;);<br /> <br />            CreateTable(<br />                &quot;dbo.dictionaryslave&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DS_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DS_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DS_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DS_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                        DS_DMID = c.Int(nullable: false),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .ForeignKey(&quot;dbo.dictionarymaster&quot;, t =&gt; t.DS_DMID, cascadeDelete: true)<br />                .Index(t =&gt; t.DS_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DS_DESCRIPTION, name: &quot;IX_Description&quot;)<br />                .Index(t =&gt; t.DS_DMID);<br /> <br />        }<br /> <br />        public override void Down()<br />        {<br />            DropForeignKey(&quot;dbo.dictionaryslave&quot;, &quot;DS_DMID&quot;, &quot;dbo.dictionarymaster&quot;);<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, new&#91;&#93; { &quot;DS_DMID&quot; });<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, &quot;IX_Description&quot;);<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, &quot;IX_Key&quot;);<br />            DropIndex(&quot;dbo.dictionarymaster&quot;, &quot;IX_Description&quot;);<br />            DropIndex(&quot;dbo.dictionarymaster&quot;, &quot;IX_Key&quot;);<br />            DropTable(&quot;dbo.dictionaryslave&quot;);<br />            DropTable(&quot;dbo.dictionarymaster&quot;);<br />        }<br />    }[/syntax]<br /><br />To samo z FluentAPI. Przy okazji. Są jakieś argumenty za/przeciw FluentAPI? Czy może to tylko kwestia indywidualnych upodobań, bo klepania nieco więcej niż przy atrybutach.<br /><br />[syntax=csharp]public class DictionaryMaster<br />    {<br />        public int Id { get; set; }<br /><br />        public string DictMasterKey { get; set; }<br /><br />        public string DictMasterDescription { get; set; }<br /><br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue1 { get; set; }<br /><br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue2 { get; set; }<br /><br />        public ICollection&lt;DictionarySlave&gt; DictionarySlaves { get; set; }<br /><br />    }<br /><br />    public class DictionaryMasterMapping : EntityTypeConfiguration&lt;DictionaryMaster&gt;<br />    {<br />        public DictionaryMasterMapping()<br />            : base()<br />        {<br />            // Mapping entity to datatable<br />            this.HasKey(e =&gt; e.Id).Property(e =&gt; e.Id).HasColumnName(&quot;ID&quot;);<br />            this.Property(e =&gt; e.DictMasterKey).HasMaxLength(10).HasColumnName(&quot;DM_KEY&quot;).HasColumnType(&quot;char&quot;);<br />            this.Property(e =&gt; e.DictMasterDescription).HasMaxLength(50).HasColumnName(&quot;DM_DESCRIPTION&quot;).HasColumnType(&quot;nvarchar&quot;);<br />            this.Property(e =&gt; e.DictMasterValue1).HasColumnName(&quot;DM_VALUE1&quot;);<br />            this.Property(e =&gt; e.DictMasterValue2).HasColumnName(&quot;DM_VALUE2&quot;);<br /><br />            // Mapping indexes<br />            this.Property(e =&gt; e.DictMasterKey).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Key&quot;) { IsUnique = true }));<br />            this.Property(e =&gt; e.DictMasterDescription).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Description&quot;)));<br /><br />            // Mapping table<br />            this.ToTable(&quot;dictionarymaster&quot;);<br />        }<br />    }<br /><br />    public partial class DictionarySlave<br />    {<br />        public int Id { get; set; }<br /><br />        public string DictSlaveKey { get; set; }<br /><br />        public string DictSlaveDescription { get; set; }<br /><br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /><br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /><br />        public DictionaryMaster DictionaryMaster { get; set; } //This is property in ForeignKey(PropertyName)<br />    }<br /><br />    public class DictionarySlaveMapping : EntityTypeConfiguration&lt;DictionarySlave&gt;<br />    {<br />        public DictionarySlaveMapping()<br />            : base()<br />        {<br />            // Mapping entity<br />            this.HasKey(e =&gt; e.Id).Property(e =&gt; e.Id).HasColumnName(&quot;ID&quot;);<br />            this.Property(e =&gt; e.DictSlaveKey).HasMaxLength(10).HasColumnName(&quot;DS_KEY&quot;).HasColumnType(&quot;char&quot;);<br />            this.Property(e =&gt; e.DictSlaveDescription).HasMaxLength(50).HasColumnName(&quot;DS_DESCRIPTION&quot;).HasColumnType(&quot;nvarchar&quot;);<br />            this.Property(e =&gt; e.DictSlaveValue1).HasColumnName(&quot;DS_VALUE1&quot;);<br />            this.Property(e =&gt; e.DictSlaveValue2).HasColumnName(&quot;DS_VALUE2&quot;);<br /><br />            // Mapping indexes<br />            this.Property(e =&gt; e.DictSlaveKey).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Key&quot;)));<br />            this.Property(e =&gt; e.DictSlaveDescription).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Description&quot;)));<br /><br />            // Mapping relations<br />            this.HasRequired(e =&gt; e.DictionaryMaster).WithMany(s =&gt; s.DictionarySlaves).Map(p =&gt; p.MapKey(&quot;DS_DMID&quot;)).WillCascadeOnDelete();<br /><br />            // Mapping table<br />            this.ToTable(&quot;dictionaryslave&quot;);<br />        }<br />    }[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=382">perlon</a> — 27 paź 2016, o 09:36</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[perlon]]></name></author>
<updated>2016-10-26T14:44:50+01:00</updated>
<published>2016-10-26T14:44:50+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172876#p172876</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172876#p172876"/>
<title type="html"><![CDATA[Re: C# Entity Framework nazwa klucza obcego]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172876#p172876"><![CDATA[
Czytałem wcześniej podany przez ciebie wpis ale chyba go nie zrozumiałem. Przeczytałem więc go jeszcze dwa razy <img src="https://forum.atnel.pl/images/smilies/icon_e_wink.gif" alt=";-)" title="Puszcza oko" /> i załapałem. Problemem było myślowe przejście z modelu relacyjne na model obiektowy i co się tam dzieje. Dzięki zadziałało podejście 1.2 [syntax=csharp]&#91;Table(&quot;dictionaryslave&quot;)&#93;<br />    public partial class DictionarySlave<br />    {<br />        &#91;Key&#93;<br />        public int Id { get; set; }<br />        <br />        &#91;Column(&quot;DS_KEY&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;), StringLength(10)&#93;<br />        public string DictSlaveKey { get; set; }<br /><br />        &#91;Column(&quot;DS_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictSlaveDescription { get; set; }<br /><br />        &#91;Column(&quot;DS_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /><br />        &#91;Column(&quot;DS_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /><br />        &#91;ForeignKey(&quot;DictionaryMaster&quot;)&#93; //Has to be a property name, not table column name<br />        &#91;Column(&quot;DS_DMID&quot;)&#93;<br />        public int DictionaryMasterId { get; set; }<br /><br />        public DictionaryMaster DictionaryMaster { get; set; }<br />    }[/syntax]<br /><br />no i migracja wygląda już tak jak chciałem <br />[syntax=csharp]CreateTable(<br />                &quot;dbo.dictionarymaster&quot;,<br />                c =&gt; new<br />                    {<br />                        Id = c.Int(nullable: false, identity: true),<br />                        DM_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DM_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DM_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DM_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                    })<br />                .PrimaryKey(t =&gt; t.Id)<br />                .Index(t =&gt; t.DM_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DM_DESCRIPTION, name: &quot;IX_Description&quot;);<br />            <br />            CreateTable(<br />                &quot;dbo.dictionaryslave&quot;,<br />                c =&gt; new<br />                    {<br />                        Id = c.Int(nullable: false, identity: true),<br />                        DS_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DS_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DS_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DS_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                        DS_DMID = c.Int(nullable: false),<br />                    })<br />                .PrimaryKey(t =&gt; t.Id)<br />                .ForeignKey(&quot;dbo.dictionarymaster&quot;, t =&gt; t.DS_DMID, cascadeDelete: true)<br />                .Index(t =&gt; t.DS_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DS_DESCRIPTION, name: &quot;IX_Description&quot;)<br />                .Index(t =&gt; t.DS_DMID);[/syntax]<br /><br />Jak będzie szło pobieranie danych to się niebawem okaże. Na razie dzięki za podpowiedź.<br /><br />Po walce z FluentAPI wyszło mi takie cość<br />[syntax=csharp]namespace sopdmodel.Models<br />{<br />    using RichardLawley.EF.AttributeConfig;<br />    using System.Data.Entity.Infrastructure.Annotations;<br />    using System.Data.Entity.ModelConfiguration;<br />    using System.Collections.Generic;<br />    using System.ComponentModel.DataAnnotations;<br />    using System.ComponentModel.DataAnnotations.Schema;<br /><br />    &#91;Table(&quot;dictionaryslave&quot;)&#93;<br />    public partial class DictionarySlave<br />    {<br />//        &#91;Key&#93;<br />        public int Id { get; set; }<br />        <br />        //&#91;Column(&quot;DS_KEY&quot;, TypeName = &quot;char&quot;)&#93;<br />        //&#91;Index(&quot;IX_Key&quot;), StringLength(10)&#93;<br />        public string DictSlaveKey { get; set; }<br /><br />        //&#91;Column(&quot;DS_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        //&#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictSlaveDescription { get; set; }<br /><br />//        &#91;Column(&quot;DS_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /><br />//        &#91;Column(&quot;DS_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /><br />        //&#91;ForeignKey(&quot;DictionaryMaster&quot;)&#93; //Has to be a property name, not table column name<br />        //&#91;Column(&quot;DS_DMID&quot;)&#93;<br />        public int DictionaryMasterId { get; set; }<br /><br />        public DictionaryMaster DictionaryMaster { get; set; }<br />    }<br /><br />    public class DictionarySlaveMapping : EntityTypeConfiguration&lt;DictionarySlave&gt;<br />    {<br />        public DictionarySlaveMapping()<br />            : base()<br />        {<br />            // Mapping entity to datatable<br />            this.HasKey(e =&gt; e.Id).Property(e =&gt; e.Id).HasColumnName(&quot;ID&quot;);<br />            this.Property(e =&gt; e.DictSlaveKey).HasColumnName(&quot;DS_KEY&quot;);<br />            this.Property(e =&gt; e.DictSlaveDescription).HasColumnName(&quot;DS_DESCRIPTION&quot;);<br />            this.Property(e =&gt; e.DictSlaveValue1).HasColumnName(&quot;DS_VALUE1&quot;);<br />            this.Property(e =&gt; e.DictSlaveValue2).HasColumnName(&quot;DS_VALUE2&quot;);<br />            // Mapping indexes<br />            this.Property(e =&gt; e.DictSlaveKey).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Key&quot;) { IsUnique = true }));<br />            this.Property(e =&gt; e.DictSlaveDescription).HasColumnAnnotation(&quot;Index&quot;, new IndexAnnotation(new IndexAttribute(&quot;IX_Description&quot;)));<br />            // Mapping relations<br />            this.HasRequired&lt;DictionaryMaster&gt;(e =&gt; e.DictionaryMaster).WithMany(s =&gt; s.DictionarySlaves).HasForeignKey(s =&gt; s.DictionaryMasterId).WillCascadeOnDelete();<br />            //  .Map(p =&gt; p.MapKey(&quot;DS_DMID&quot;)<br />            this.ToTable(&quot;dictionaryslave&quot;);<br />        }<br />    }[/syntax]<br />[syntax=csharp]CreateTable(<br />                &quot;dbo.dictionaryslave&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DS_KEY = c.String(unicode: false),<br />                        DS_DESCRIPTION = c.String(unicode: false),<br />                        DS_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DS_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                        DictionaryMasterId = c.Int(nullable: false),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .ForeignKey(&quot;dbo.dictionarymaster&quot;, t =&gt; t.DictionaryMasterId, cascadeDelete: true)<br />                .Index(t =&gt; t.DS_KEY, unique: true, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DS_DESCRIPTION, name: &quot;IX_Description&quot;)<br />                .Index(t =&gt; t.DictionaryMasterId);[/syntax]<br />Tylko jak zmapować DictionaryMasterId na kolumnę DS_DMID<br />Mogę zrobić tak ale to jest jakaś masakra.<br />[syntax=csharp]//&#91;ForeignKey(&quot;DictionaryMaster&quot;)&#93; //Has to be a property name, not table column name<br />        //&#91;Column(&quot;DS_DMID&quot;)&#93;<br />        public int DS_DMID { get; set; }<br /><br />        public DictionaryMaster DictionaryMaster { get; set; }<br /><br /><br />this.HasRequired&lt;DictionaryMaster&gt;(e =&gt; e.DictionaryMaster).WithMany(s =&gt; s.DictionarySlaves).HasForeignKey(s =&gt; s.DS_DMID).WillCascadeOnDelete();[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=382">perlon</a> — 26 paź 2016, o 14:44</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[RafPe]]></name></author>
<updated>2016-10-26T13:00:23+01:00</updated>
<published>2016-10-26T13:00:23+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172855#p172855</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172855#p172855"/>
<title type="html"><![CDATA[Re: C# Entity Framework nazwa klucza obcego]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172855#p172855"><![CDATA[
Dawno temu bawilem sie EF - ale Tak na szybko rzuc okiem <!-- m --><a class="postlink" href="http://stackoverflow.com/a/11148834/2476347" >http://stackoverflow.com/a/11148834/2476347</a><!-- m --> uzywajac wlasnie tego podejscia powienenes osiagnac co potrzebujesz. <br /><br />Daj znac czy cos pomoglo a jak nie to postaram sie cos wiecej zrobic jak bede pozniej w domu <img src="https://forum.atnel.pl/images/smilies/icon_e_wink.gif" alt=";)" title="Puszcza oko" /><p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=4344">RafPe</a> — 26 paź 2016, o 13:00</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[perlon]]></name></author>
<updated>2016-10-26T12:54:21+01:00</updated>
<published>2016-10-26T12:54:21+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172852#p172852</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172852#p172852"/>
<title type="html"><![CDATA[C# Entity Framework nazwa klucza obcego]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=16631&amp;p=172852#p172852"><![CDATA[
Witam.<br />Na początek zaznaczam, że jestem bardzo początkujący w temacie dlatego proszę o wyrozumiałość.<br />Piszę programik z EF6 Code First. Mam Dwie klasy DictionaryMaster i DictionarySlave. Jak się można domyśleć są powiązane relacją 1+M. Ppotrzebuję ustawić konkretną moją nazwę pola obsługującego relacje. O co dokładnie chodzi wyjaśniam w dalszej części.<br /><br />Klasy wyglądają następująco<br />[syntax=csharp]namespace sopd2.Models<br />{<br />    using RichardLawley.EF.AttributeConfig;<br />    using System.Collections.Generic;<br />    using System.ComponentModel.DataAnnotations;<br />    using System.ComponentModel.DataAnnotations.Schema;<br /><br />    &#91;Table(&quot;dictionarymaster&quot;)&#93;<br />    public class DictionaryMaster<br />    {<br />        &#91;Key&#93;<br />        public int ID { get; set; }<br /><br />        &#91;Column(&quot;DM_KEY&quot;, TypeName=&quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;),StringLength(10)&#93;<br />        public string DictMasterKey { get; set; }<br /><br />        &#91;Column(&quot;DM_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictMasterDescription { get; set; }<br /><br />        &#91;Column(&quot;DM_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue1 { get; set; }<br /><br />        &#91;Column(&quot;DM_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictMasterValue2 { get; set; }<br /><br />        public ICollection&lt;DictionarySlave&gt; DictionarySlaves { get; set; }<br /><br />    }<br /><br />    &#91;Table(&quot;dictionaryslave&quot;)&#93;<br />    public partial class DictionarySlave<br />    {<br />        &#91;Key&#93;<br />        public int ID { get; set; }<br /><br />        &#91;Column(&quot;DS_KEY&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;Index(&quot;IX_Key&quot;), StringLength(10)&#93;<br />        public string DictSlaveKey { get; set; }<br /><br />        &#91;Column(&quot;DS_DESCRIPTION&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Index(&quot;IX_Description&quot;), StringLength(50)&#93;<br />        public string DictSlaveDescription { get; set; }<br /><br />        &#91;Column(&quot;DS_VALUE1&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue1 { get; set; }<br /><br />        &#91;Column(&quot;DS_VALUE2&quot;)&#93;<br />        &#91;DecimalPrecision(11, 2)&#93;<br />        public decimal? DictSlaveValue2 { get; set; }<br /><br />    }<br />}[/syntax] <br /><br />Migracja tworzy mi następujący kod tworzący tabele<br />[syntax=csharp]namespace sopd2.Migrations<br />{<br />    using System;<br />    using System.Data.Entity.Migrations;<br />    <br />    public partial class des01 : DbMigration<br />    {<br />        public override void Up()<br />        {           <br />            CreateTable(<br />                &quot;dbo.dictionarymaster&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DM_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DM_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DM_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DM_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .Index(t =&gt; t.DM_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DM_DESCRIPTION, name: &quot;IX_Description&quot;);<br />            <br />            CreateTable(<br />                &quot;dbo.dictionaryslave&quot;,<br />                c =&gt; new<br />                    {<br />                        ID = c.Int(nullable: false, identity: true),<br />                        DS_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: &quot;char&quot;),<br />                        DS_DESCRIPTION = c.String(maxLength: 50, storeType: &quot;nvarchar&quot;),<br />                        DS_VALUE1 = c.Decimal(precision: 11, scale: 2),<br />                        DS_VALUE2 = c.Decimal(precision: 11, scale: 2),<br />                        DictionaryMaster_ID = c.Int(),<br />                    })<br />                .PrimaryKey(t =&gt; t.ID)<br />                .ForeignKey(&quot;dbo.dictionarymaster&quot;, t =&gt; t.DictionaryMaster_ID)<br />                .Index(t =&gt; t.DS_KEY, name: &quot;IX_Key&quot;)<br />                .Index(t =&gt; t.DS_DESCRIPTION, name: &quot;IX_Description&quot;)<br />                .Index(t =&gt; t.DictionaryMaster_ID);<br />            <br />        }<br />        <br />        public override void Down()<br />        {<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, new&#91;&#93; { &quot;DictionaryMaster_ID&quot; });<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, &quot;IX_Description&quot;);<br />            DropIndex(&quot;dbo.dictionaryslave&quot;, &quot;IX_Key&quot;);<br />            DropIndex(&quot;dbo.dictionarymaster&quot;, &quot;IX_Description&quot;);<br />            DropIndex(&quot;dbo.dictionarymaster&quot;, &quot;IX_Key&quot;);<br />        }<br />    }<br />}[/syntax]<br /><br />Jak widać w tabeli 'dictionaryslave' niejako od zaplecza zostało stworzone pole 'DictionaryMaster_ID' jako klucz obcy do obsługi relacji 1+M. Widać również że jego nazwa nie pasuje do przyjętej konwencji nazewnictwa kolumn w tabeli. Chciałbym je ustawić na 'DS_DMID'. Próbowałem zdefiniować kolejną właściwość w klasie DictionarySlave ale nie udało mi się jej podpiąć jako ForeinKey<br />[syntax=csharp]&#91;ForeignKey(&quot;DS_DMID&quot;)&#93;<br />        public int DictMasterID { get; set; }[/syntax]<br /><br />Mam inny zestaw klas z relacją M+N i poradziłem sobie z podobnym tematem następująco :<br />[syntax=csharp]&#91;Table(&quot;users&quot;)&#93;<br />    public partial class User<br />    {<br />        &#91;Key&#93;<br />        public int ID { get; set; }<br /><br />        &#91;Column(&quot;US_LOGIN&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;Required&#93;<br />        &#91;StringLength(10)&#93;<br />        public string Login { get; set; }<br /><br />        &#91;Column(&quot;US_NAME&quot;,TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Required&#93;<br />        &#91;StringLength(30)&#93;<br />        public string SurName { get; set; }<br /><br />        &#91;Column(&quot;US_FNAME&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Required&#93;<br />        &#91;StringLength(30)&#93;<br />        public string FirstName { get; set; }<br /><br />        &#91;Column(&quot;US_EMAIL&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Required&#93;<br />        &#91;StringLength(30)&#93;<br />        public string Email { get; set; }<br /><br />        &#91;Column(&quot;US_TIMESTAMP&quot;)&#93;<br />        public DateTime? TimeStamp { get; set; }<br /><br />        &#91;Column(&quot;US_PRIVILEGE&quot;, TypeName = &quot;bigint&quot;)&#93;<br />        public long Privilege { get; set; }<br /><br />        &#91;Column(&quot;US_HASH&quot;, TypeName = &quot;char&quot;)&#93;<br />        &#91;StringLength(50)&#93;<br />        public string Hash { get; set; }<br /><br />        public virtual ICollection&lt;UserGroup&gt; UserGroups { get; set; }<br />    }<br /><br />    &#91;Table(&quot;usergroups&quot;)&#93;<br />    public partial class UserGroup<br />    {<br />        &#91;Key&#93;<br />        public int ID { get; set; }<br /><br />        &#91;Column(&quot;UG_NAME&quot;, TypeName = &quot;nvarchar&quot;)&#93;<br />        &#91;Required&#93;<br />        &#91;Index(&quot;IX_Name&quot;), StringLength(50)&#93;<br />        public string UserGroupName { get; set; }<br /><br />        public ICollection&lt;User&gt; Users { get; set; }<br />    }[/syntax]<br /><br />I w metodzie OnModelCreating coś takiego co załatwia mi zmianę nazwy tabeli jak również własne nazwy kolumn i gitara. Mniej więcej rozumiem co poniższe robi ale niestety nie potrafię poniższego zaimplementować w tabeli podrzędnej DictionarySlave w relacji 1+M.<br /> <br />[syntax=csharp]protected override void OnModelCreating(DbModelBuilder modelBuilder)<br />        {<br />            base.OnModelCreating(modelBuilder);<br /><br />            modelBuilder.Conventions.Remove&lt;System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention&gt;();<br />            // Add conventions for Precision Attributes<br />            modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());<br />            modelBuilder.Conventions.Add(new DateTimePrecisionAttributeConvention());<br /><br />            modelBuilder.Entity&lt;User&gt;().HasMany(x =&gt; x.UserGroups).WithMany(y =&gt; y.Users)<br />                .Map(m =&gt; <br />                {<br />                    m.ToTable(&quot;usergroupmembers&quot;);<br />                    m.MapLeftKey(&quot;GM_USID&quot;);<br />                    m.MapRightKey(&quot;GM_GRID&quot;);<br />                });<br />        }[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=382">perlon</a> — 26 paź 2016, o 12:54</p><hr />
]]></content>
</entry>
</feed>