了解公共属性列表 - C# MVC 最佳做法



我正在尝试找出最佳编码实践和性能优化。我正在学习 C# MVC 并创建一个图片项目来使用我正在学习的内容。问题来了。

我目前有

public long PictureID { get; set; }
    public int UserID { get; set; }
    public string File { get; set; }
    public string Category { get; set; }
    public string Breed { get; set; }
    public DateTime Posted { get; set; }
但是

我认为类别和品种应该是一个列表,但是我不确定在哪里正确实现它。

public long PictureID { get; set; }
    public int UserID { get; set; }
    public string File { get; set; }
    public List<Category> Animal { get; set; }
    public List<BreedType> Breed { get; set; }
    public DateTime Posted { get; set; }

该类别将保持静态,因为值将是狗猫鸟马等,但是品种将根据所选类别而变化

我是在类中还是在单独的类中列出类别和品种选项?另一方面,我应该为每个类别品种创建一个表格,如 狗品种 猫品种 等?

感谢任何可以提供帮助的人。

你可以有这样的结构:一对多关系 .对于类别和品种

class Pic
{ 
public Pic()
{
Categories = new List();
}
    public long PictureID { get; set; }
    public int UserID { get; set; }
    public string File { get; set; }
    public ICollection<Category> Categories{ get; set; }
    public DateTime Posted { get; set; }
}

为类别制作类并在那里实施品种.

class Category
{
public Category()
{
Breeds = new List();
}
public ICollection<Breed> Breeds {get;set;}
}

通过这种方式,您可以实现您的关系。我希望这有帮助..

我会这样组织表格

Category
- Id
- Name
Breed
- Id
- Name
- CategoryId
Stuff
- PictureId
- UserId
- File
- BreedId
- Posted

像这样的模型,为什么你想要一个类别或品种列表?

public class Category
{
    public int Id {get;set;}
    public int Name {get;set;}
    public virtual ICollection<Breed> Breeds {get;set;}
}
public class Breed
{
    public int Id {get;set;}
    public int Name {get;set;}
    public int CategoryId {get;set;}
    public virtual Category Category {get;set;}
}

public class Stuff
{
    public int PictureId {get;set;}
    public int UserId {get;set;}
    public string File {get;set;}
    public int BreedId {get;set;}
    public DateTime Posted {get;set;}
    public virtual Breed Breed {get;set;}
}

如果我想要品种的类别,我可以做类似的事情

stuff.Breed.Category.Name

如果这非常详细,请使用重要的属性创建一个 ViewModel,并在...

最新更新