0

I have a model and i want to put an extra field which can be populated form the same model. IE: Categories and and sub-categories.

In my example, visitor can add an filetype but if file type is under an another file type, he can choose, But i cant work it out. Below you can see my model.

 public class HrFileType
    {
        [Key]
        public int Id { get; set; }
        [Display(Name = "Dosya Adı")]
        public int Name { get; set; }

        public int? HrFileTypeId { get; set; }
        public virtual HrFileType HrFileType2 { get; set; }
    }

2 Answers 2

1

You just need to add a ForeignKeyAttribute like below:

public class HrFileType
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "Dosya Adı")]
    public int Name { get; set; }

    public int? HrFileTypeId { get; set; }

    [ForeignKey("HrFileTypeId")]
    public virtual HrFileType HrFileType2 { get; set; }
}

You can also use fluent API to achieve this:

public class HrFileType
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "Dosya Adı")]
    public int Name { get; set; }

    public int? HrFileTypeId { get; set; }
    public virtual HrFileType HrFileType2 { get; set; }
}

public class YourDbContext : DbContext
{
    public DbSet<HrFileType> HrFileTypes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //
        modelBuilder.Entity<HrFileType>()
                    .HasOptional(c => c.HrFileType2)
                    .WithMany()
                    .HasForeignKey(c => c.HrFileTypeId);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is what i am trying to do :)
0

Have you tried listing the other file types?

 public class HrFileType
    {
        [Key]
        public int Id { get; set; }
        [Display(Name = "Dosya Adı")]
        public int Name { get; set; }

        public List<HrFileType> RelatedTypes { get; set; }
    }

then using Entity Frameworks fluent API in the DbContext, try explicitly declaring a many to many map.

modelbuilder.Entity<HrFileType>().HasMany(x => x.RelatedTypes).WithMany();

I'd be very interested to see if this works. It's the only logical solution I can think of without having some kind of parent class.

4 Comments

Sorry not working,Error 1 The type arguments for method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<ModulericaV1.Areas.Hr.Models.HrFileType>.HasMany<TTargetEntity>(System.Linq.Expressions.Expression<System.Func<ModulericaV1.Areas.Hr.Models.HrFileType,System.Collections.Generic.ICollection<TTargetEntity>>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. c:\users\umutg\documents\visual studio 2013\Projects\ModulericaV1\ModulericaV1\Models\ModulericaDbContext.cs 20 9 ModulericaV1
I've just made a minor change to the fluent api which you could test. My theory behind the many to many relationship is to force EF to create a separate table. Out of interest how many related file types could there be?
actually , i do not want it to create a separate file. It can be more tan 30 :S
Are you persisting to a sql database? It wouldn't be a new file, only a table holding the relational mapping. Did you try the update?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.