2

ihave 3 models user,ArticleComments and UserSubsription .. im trying to make 3 prop as composite key and two of them are foreign key references to two different tables but i got error while trying to enable-migrations

User.CS

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using WebAppInternetApp.Models;

namespace WebAppInternetApp.Models
{
    public class User
    {

        public User()
        {
            this.WriterIDs = new HashSet<JobArticle>();
            this.AdminIDs = new HashSet<JobArticle>();
            this.UserIDs = new HashSet<ArticleComments>();
            this.UserIDss = new HashSet<UserQuestion>();
            this.UseerIDsss = new HashSet<UserSubscription>();
        }

        [Key]
        public int UID { get; set; }

        [Required]
        [StringLength(20, MinimumLength = 2)]
        public string FName { set; get; }

        [Required]
        [StringLength(20, MinimumLength = 2)]
        public string LName { set; get; }

        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { set; get; }

        [Required]
        [StringLength(25, MinimumLength = 8)]
        public string Password { set; get; }

        [Required]
        public bool Status { set; get; }

        [Required]
        [Phone]
        public string PhoneNo { set; get; }

        [Column(TypeName = "image")]
        public byte[] UserImg { set; get; }

        public virtual ICollection<JobArticle> WriterIDs { set; get; }
        public virtual ICollection<JobArticle> AdminIDs { set; get; }
        public virtual ICollection<ArticleComments> UserIDs { set; get; }
        public virtual ICollection<UserQuestion> UserIDss { set; get; }
        public virtual ICollection<UserSubscription> UseerIDsss { set; get; }

        public virtual UserType usertypeIDs { set; get; }



    }
}

Article Comments.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebAppInternetApp.Models
{
    public class ArticleComments
    {
        [Required]
        public string Comment { set; get; }
        [Key]
        [Column(Order = 0)]
        public DateTime CommentTime { set; get; }
        [Key]
        [Column(Order = 1)]
        [ForeignKey("User")]
        public virtual User UserID { set; get; }
        [Key]
        [Column(Order = 2)]
        [ForeignKey("JobArticle")]
        public virtual JobArticle ArticleID { set; get; }
    }
}

UserSubsription.CS

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using WebAppInternetApp.Models;

namespace WebAppInternetApp.Models
{
    public class UserSubscription
    {
        [Key]
        [Column(Order = 0)]
        [ForeignKey("User")]
        public virtual User UserIDs { set; get; }

        [Key]
        [Column(Order = 1)]
        [ForeignKey("JobCategory")]
        public virtual JobCategory SubscriptID { set; get; }
    }
}
  • What error did you get? – malkassem Apr 24 '16 at 12:21
  • The ForeignKeyAttribute on property 'ArticleID' on type 'WebAppInternetApp.Models.ArticleComments' is not valid. The foreign key name 'JobArticle' was not found on the dependent type 'WebAppInternetApp.Models.ArticleComments'. The Name value should be a comma separated list of foreign key property names. – Karim Ahmed Apr 24 '16 at 12:25

1 Answers1

0

Base on Microsoft description of the ForeignKeyAttribute:

If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s). If a navigation property has multiple foreign keys, use comma to separate the list of foreign key names

In your code, you don't define the foreign key property. You only define the navigation properties.

Check this article to see how to use the ForeignKey attribute:

Understanding ForeignKey attribute in entity framework code first

For example, your UserSubscription should look something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using WebAppInternetApp.Models;

namespace WebAppInternetApp.Models
{
    public class UserSubscription
    {
        [Key]
        [Column(Order = 0)]
        [ForeignKey("User")]
        public int UserIDs { set; get; }

        [Key]
        [Column(Order = 1)]
        [ForeignKey("JobCategory")]
        public int SubscriptID { set; get; }

        // those are the navigation properties
        public virtual User User { set; get; }
        public virtual JobCategory JobCategory { set; get; }
    }
}

And ArticleComments could be like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WebAppInternetApp.Models
{
    public class ArticleComments
    {
        [Required]
        public string Comment { set; get; }

        [Key]
        [Column(Order = 0)]
        public DateTime CommentTime { set; get; }

        [Key]
        [Column(Order = 1)]
        [ForeignKey("User")]
        public int UserID { set; get; }

        [Key]
        [Column(Order = 2)]
        [ForeignKey("JobArticle")]
        public int ArticleID { set; get; }

        public virtual User User { set; get; }
        public virtual JobArticle JobArticle { set; get; }

    }
}
Community
  • 1
  • 1
malkassem
  • 1,937
  • 1
  • 20
  • 39
  • if i delete foreign keys i got this message "WebAppInternetApp.Models.UserSubscription: : EntityType 'UserSubscription' has no key defined. Define the key for this EntityType. UserSubscriptions: EntityType: EntitySet 'UserSubscriptions' is based on type 'UserSubscription' that has no keys defined. " – Karim Ahmed Apr 24 '16 at 12:54
  • but it will make two property for userid and article id ! – Karim Ahmed Apr 24 '16 at 13:34
  • and i got this message while trying this WebAppInternetApp.Models.UserSubscription: : EntityType 'UserSubscription' has no key defined. Define the key for this EntityType. UserSubscriptions: EntityType: EntitySet 'UserSubscriptions' is based on type 'UserSubscription' that has no keys defined. – Karim Ahmed Apr 24 '16 at 13:35