logo
down
shadow

Lookup table with EF Codefirst


Lookup table with EF Codefirst

By : user2952275
Date : November 19 2020, 12:41 AM
I wish did fix the issue. If you want RelatedResource table, than you want many-to-many relationship. You can achieve this in next way
code :
public class Resource
{
    public int ResourceId { get; set; }
    public string ResourceName { get; set; }
    public int Category { get; set; }

    [InverseProperty("Resoure")]
    public virtual ICollection<RelatedResource> RelatedResource { get; set; }
    [InverseProperty("ResourceRelated")]
    public virtual ICollection<RelatedResource> ResourceRelated { get; set; }
}

public class RelatedResource
{
    [Key]
    [Column(Order = 1)]
    [ForeignKey("Resoure")]
    public int ResourceId { get; set; }
    [Key]
    [Column(Order = 2)]
    [ForeignKey("ResourceRelated")]
    public int ResourceRelatedId { get; set; }

    public virtual Resource Resoure { get; set; }
    public virtual Resource ResourceRelated { get; set; }
}
public class Resource
{
    public int ResourceId { get; set; }
    public string ResourceName { get; set; }
    public int Category { get; set; }
    public virtual ICollection<Resource> RelatedResource { get; set; }
    public virtual ICollection<Resource> ResourceRelated { get; set; }
}
internal class ResourceConfiguration : EntityTypeConfiguration<Resource>
{
    public ResourceConfiguration()
    {
        HasMany(s => s.ResourceRelated).WithMany(c => c.RelatedResource)
            .Map(cs => { 
                cs.MapLeftKey("ResourceId"); 
                cs.MapRightKey("RelatedResourceId");
                cs.ToTable("RelatedResource"); }
            );
    }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    ///other configuration code
    modelBuilder.Configurations.Add(new ResourceConfiguration());
}
public class Resource
{
    public int ResourceId { get; set; }
    public string ResourceName { get; set; }
    public int Category { get; set; }
    public int? ResourceRelatedId { get; set; }

    public Resource ResourceRelated { get; set; }
    [ForeignKey("ResourceRelatedId")]
    public virtual ICollection<Resource> RelatedResource { get; set; }
}


Share : facebook icon twitter icon
Empty lookup table vs non-matching lookup table (lookup transform)

Empty lookup table vs non-matching lookup table (lookup transform)


By : Greg Finn
Date : March 29 2020, 07:55 AM
Does that help You could get #2: Package failure. It would not be able to find the row in the lookup table (since it's empty).
Edit: I should say that if you set the Error Configuration to Ignore Failure, you will get an empty rowset.
Table name with dot in CodeFirst

Table name with dot in CodeFirst


By : palak kothari
Date : March 29 2020, 07:55 AM
To fix this issue Apparently the DDL commands don't properly wrap the object identifiers.
Try wrapping the Table name in [MyNewSubProject.Table1].
code :
modelBuilder.Entity<Entity>().ToTable("Table1", "MyNewSubProject");
EF CodeFirst: DropCreateDatabaseIfModelChanges except one table

EF CodeFirst: DropCreateDatabaseIfModelChanges except one table


By : Sigal Dor
Date : March 29 2020, 07:55 AM
wish helps you No, there is no way to exclude a table, because as the name implies it will Drop the whole database.
However you can inherit from the intializer and override the seed method to reinsert your data:
code :
public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
    protected override void Seed(MyContext context)
    {
        //insert data to tables through context
    }
}
public class MyComplexInitializer : IDatabaseInitializer<MyContext>
{ 
    public void InitializeDatabase(MyContext context)
    {
        if (context.Database.Exists())
        {
             if (context.Database.CompatibleWithModel(true))
                 return;
             //do something bofore delete
             context.Database.Delete();
        }
        context.Database.Create();
        //add some data to context
        context.SaveChanges();                 
    }
}
Table Not Generated Using CodeFirst

Table Not Generated Using CodeFirst


By : Валерий Дюмин
Date : March 29 2020, 07:55 AM
may help you . Yes, it is because of inheritance. By default TPH (table per hierarchy) inheritance is used. So, you have single Company table, which includes all properties of classes in hierarchy. Add ToTable mapping to implement TPT (table per type) inheritance.
code :
modelBuilder.Entity<Customer>()
            .ToTable("Customer");
MySQL Lookup Table + Multiple Constraints in one Lookup Table

MySQL Lookup Table + Multiple Constraints in one Lookup Table


By : Akihiro Matsushita
Date : March 29 2020, 07:55 AM
this one helps. If by constraint you mean lookup tables, then no, I would not combine them into a single table. If one of the fields has 2 possible values and another one has 3, then for the first one you would still have 3 choices available in the lookup table, even if the 3rd choice is linked to null.
If you are worried about the number of lookup tables, then consider using mysql's enum data type instead.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org