Witam wszystkich i kłaniam się nisko jako że to mój pierwszy post na forum.
Na początek zaznaczam, że jestem bardzo początkujący w temacie dlatego proszę o wyrozumiałość.
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.
Klasy wyglądają następująco
namespace sopd2.Models
{
using RichardLawley.EF.AttributeConfig;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("dictionarymaster")]
public class DictionaryMaster
{
[Key]
public int ID { get; set; }
[Column("DM_KEY", TypeName="char")]
[Index("IX_Key"),StringLength(10)]
public string DictMasterKey { get; set; }
[Column("DM_DESCRIPTION", TypeName = "nvarchar")]
[Index("IX_Description"), StringLength(50)]
public string DictMasterDescription { get; set; }
[Column("DM_VALUE1")]
[DecimalPrecision(11, 2)]
public decimal? DictMasterValue1 { get; set; }
[Column("DM_VALUE2")]
[DecimalPrecision(11, 2)]
public decimal? DictMasterValue2 { get; set; }
public ICollection<DictionarySlave> DictionarySlaves { get; set; }
}
[Table("dictionaryslave")]
public partial class DictionarySlave
{
[Key]
public int ID { get; set; }
[Column("DS_KEY", TypeName = "char")]
[Index("IX_Key"), StringLength(10)]
public string DictSlaveKey { get; set; }
[Column("DS_DESCRIPTION", TypeName = "nvarchar")]
[Index("IX_Description"), StringLength(50)]
public string DictSlaveDescription { get; set; }
[Column("DS_VALUE1")]
[DecimalPrecision(11, 2)]
public decimal? DictSlaveValue1 { get; set; }
[Column("DS_VALUE2")]
[DecimalPrecision(11, 2)]
public decimal? DictSlaveValue2 { get; set; }
}
}
Migracja tworzy mi następujący kod tworzący tabele
namespace sopd2.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class des01 : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.dictionarymaster",
c => new
{
ID = c.Int(nullable: false, identity: true),
DM_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: "char"),
DM_DESCRIPTION = c.String(maxLength: 50, storeType: "nvarchar"),
DM_VALUE1 = c.Decimal(precision: 11, scale: 2),
DM_VALUE2 = c.Decimal(precision: 11, scale: 2),
})
.PrimaryKey(t => t.ID)
.Index(t => t.DM_KEY, name: "IX_Key")
.Index(t => t.DM_DESCRIPTION, name: "IX_Description");
CreateTable(
"dbo.dictionaryslave",
c => new
{
ID = c.Int(nullable: false, identity: true),
DS_KEY = c.String(maxLength: 10, fixedLength: true, unicode: false, storeType: "char"),
DS_DESCRIPTION = c.String(maxLength: 50, storeType: "nvarchar"),
DS_VALUE1 = c.Decimal(precision: 11, scale: 2),
DS_VALUE2 = c.Decimal(precision: 11, scale: 2),
DictionaryMaster_ID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.dictionarymaster", t => t.DictionaryMaster_ID)
.Index(t => t.DS_KEY, name: "IX_Key")
.Index(t => t.DS_DESCRIPTION, name: "IX_Description")
.Index(t => t.DictionaryMaster_ID);
}
public override void Down()
{
DropIndex("dbo.dictionaryslave", new[] { "DictionaryMaster_ID" });
DropIndex("dbo.dictionaryslave", "IX_Description");
DropIndex("dbo.dictionaryslave", "IX_Key");
DropIndex("dbo.dictionarymaster", "IX_Description");
DropIndex("dbo.dictionarymaster", "IX_Key");
}
}
}
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
[ForeignKey("DS_DMID")]
public int DictMasterID { get; set; }
Mam inny zestaw klas z relacją M+N i poradziłem sobie z podobnym tematem następująco :
[Table("users")]
public partial class User
{
[Key]
public int ID { get; set; }
[Column("US_LOGIN", TypeName = "char")]
[Required]
[StringLength(10)]
public string Login { get; set; }
[Column("US_NAME",TypeName = "nvarchar")]
[Required]
[StringLength(30)]
public string SurName { get; set; }
[Column("US_FNAME", TypeName = "nvarchar")]
[Required]
[StringLength(30)]
public string FirstName { get; set; }
[Column("US_EMAIL", TypeName = "nvarchar")]
[Required]
[StringLength(30)]
public string Email { get; set; }
[Column("US_TIMESTAMP")]
public DateTime? TimeStamp { get; set; }
[Column("US_PRIVILEGE", TypeName = "bigint")]
public long Privilege { get; set; }
[Column("US_HASH", TypeName = "char")]
[StringLength(50)]
public string Hash { get; set; }
public virtual ICollection<UserGroup> UserGroups { get; set; }
}
[Table("usergroups")]
public partial class UserGroup
{
[Key]
public int ID { get; set; }
[Column("UG_NAME", TypeName = "nvarchar")]
[Required]
[Index("IX_Name"), StringLength(50)]
public string UserGroupName { get; set; }
public ICollection<User> Users { get; set; }
}
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.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
// Add conventions for Precision Attributes
modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
modelBuilder.Conventions.Add(new DateTimePrecisionAttributeConvention());
modelBuilder.Entity<User>().HasMany(x => x.UserGroups).WithMany(y => y.Users)
.Map(m =>
{
m.ToTable("usergroupmembers");
m.MapLeftKey("GM_USID");
m.MapRightKey("GM_GRID");
});
}