The implementation of a multi-state model in MVC - c#

I want to create a QUESTION model in the test system.
The question may be different (with text, with picture, etc)
The answer may be different (textfield, checkboxes, etc)
How to implement this in MVC correctly?
Picture for understanding

Business/Domain Objects:
public class Question {
public int Id { get; set; }
public string Text { get; set; }
public string ImagePath { get; set; }
public IList<Answer> Answers { get; set; }
public Answer CorrectAnswer { get; set; }
}
public class Answer {
public int Id { get; set; }
public string Text { get; set; }
}
View Model:
public class QuestionViewModel {
public Question Question { get; set; }
}

Related

EF Core OwnedTypes on EducationModule entity. How to configure?

I want to reduce complexity of entities which are responsible for education modules (tests).
OwnedTypes from EF Core are perfect for me, but I'm not sure how to configure these entities properly.
Here are entities:
EducationModule - name of test, level, description and collection of Questions
public class EducationModule : AuditableEntity
{
public EducationModuleId EducationModuleId { get; set; }
public EducationModuleName Name { get; set; }
public EducationModuleLevel Level { get; set; }
public EducationModuleDescription Description { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
Question - question text, explanation and collection of Answers,
public class Question : AuditableEntity
{
public QuestionId QuestionId { get; set; }
public QuestionText QuestionText { get; set; }
public QuestionExplanation QuestionExplanation { get; set; }
public virtual ICollection<QuestionAnswer> Answers { get; set; }
public EducationModuleId EducationModuleId { get; set; }
public virtual EducationModule EducationModule { get; set; }
}
QuestionAnswer - answer text, bool indicator to tell if answer is correct
public class QuestionAnswer : AuditableEntity
{
public QuestionAnswerId QuestionAnswerId { get; set; }
public QuestionAnswerText Text { get; set; }
public Correct IsCorrect { get; set; }
public QuestionId QuestionId { get; set; }
public virtual Question Question { get; set; }
}
How to create EntityTypeConfiguration in order to get EducationModule as main entity and Questions as owned type (Questions having Answers as owned type)? What should i focus on? How to approach and solve this?

I try to make filter checkbox with razor

Can someone help my with business logic when I try to find products in my bd list with properties from ProductFilterVM or give my advice where I can find solution.
public class ProductFilterVM
{
public string? Name { get; set; }
// other properties relating to a product that you may want to display the view - e.g. Description
public IEnumerable<FilterCategoryVM>? FilterCategories { get; set; }
}
public class FilterCategoryVM
{
public int Id { get; set; }
public string? Name { get; set; }
public List<FilterSelectionVM>? Selections { get; set; }
}
public class FilterSelectionVM
{
public int Id { get; set; }
public string? Name { get; set; }
}

Entity Framework Core - One to Many without referring source model

I am making an application that needs some metadata-handling and some model linking.
Let's say I have ModelA and ModelB. I want to have a Entity that stores a list of those two, without them strict knows about that list.
public class ModelA
{
public int Id { get; set; }
public string Title { get; set; }
}
public class ModelB
{
public int Id { get; set; }
public string Title { get; set; }
}
public class ModelTemplate
{
public int Id { get; set; }
public string Title { get; set; }
public List<ModelA> ModelAs { get; set; }
public List<ModelB> ModelBs { get; set; }
}
When looking at the migrations it looks like it want to put a reference from ModelTemplate->Id into ModelA and ModelB, but they can be present in mulitple of those lists.
Any tips on where I should look for examples?

Navigation Property Issue With Entity Framework (ASP.NET MVC)

My database structure looks like this: Categories table, Questions table and Answers table. Questions have a CategoryID, and Answers have an AnswerID. When trying to access the navigation property "Answers" in my view, I receive the following error:
"'System.Data.Entity.Core.EntityCommandExecutionException' occurred in
EntityFramework.SqlServer.dll but was not handled in user code" Which
is accompanied by:
"Invalid column name 'Question_ID'."
Here's what my classes look like:
public class Category
{
public int ID { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public virtual List<Question> Questions { get; set; }
}
For Question:
public class Question
{
public int ID { get; set; }
public int CategoryID { get; set; }
public string Text { get; set; }
public virtual List<Answer> Answers { get; set; }
public virtual Category Category { get; set; }
}
And for Answer:
public class Answer
{
public int ID { get; set; }
public int QuestionID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
public virtual Question Question { get; set; }
}
And my Database uses exactly the same field names, with foreign keys applied. I have tried so many variations on the names, including changing the foreign keys to 'Question_ID'and 'Category_ID' but no luck. I've also tried this solution: Code First conventions confusion but it didn't work. Anybody know where I'm going wrong?
I believe the problem is with your answer class. Try this...
public class Answer
{
public int ID { get; set; }
public string Text { get; set; }
public int Correct { get; set; }
[ForeignKey("Question")]
public int QuestionID { get; set; }
public virtual Question Question { get; set; }
}

What is the best way to set up inheritance to avoid future redundancy in code? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have 7 classes:
public class Entity
{
public int Id { get; set; }
}
public class Product : ????
{
// Contructor
public Product()
{
Photos = new HashSet<PhotoSource>();
ProductFeatures = new HashSet<ProductFeature>();
}
// Primitives
public string ProductName { get; set; }
public string InternalSKU { get; set; }
public string ModelNumber { get; set; }
public string Description { get; set; }
public int QtyPerUnit { get; set; }
public double UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public int UnitsOnOrder { get; set; }
public int? ReOrderLevel { get; set; }
public string Warranty { get; set; }
// Foreign Keys
public int SubCategoryID { get; set; }
public int VendorId { get; set; }
// Navigation Properties
// Classes
[ForeignKey("SubCategoryID")]
public virtual SubCategory SubCategory { get; set; }
[ForeignKey("VendorId")]
public virtual Vendor Vendor { get; set; }
// Collections
public virtual ICollection<PhotoSource> Photos { get; set; }
public virtual ICollection<ProductFeature> ProductFeatures { get; set; }
}
public class ProductSeasonal : ????
{
// Primitives
public int? OffSeasonDiscount { get; set; }
public DateTime SeasonStartDate { get; set; }
public DateTime SeasonEndDate { get; set; }
public int? QtyLimitedTo { get; set; }
}
public class ProductDiscontinued : ????
{
// Primitives
public DateTime DiscontinuedDate { get; set; }
public int DiscontinuedDisount { get; set; }
}
public class Supply : ????
{
// Primitives
public String UnitMeasurement { get; set; }
}
public class Part : ????
{
// Primitives
public String UnitMeasurement { get; set; }
}
public class Vehicle : ????
{
// Constructor
public Vehicle()
{
ExteriorFeatures = new HashSet<ProductFeature>();
InteriorFeatures = new HashSet<ProductFeature>();
SafetyFeatures = new HashSet<ProductFeature>();
}
// Primitives
public string VIN { get; set; }
public int Year { get; set; }
public int CylinderSize { get; set; }
public double EngineSize { get; set; }
public string StyleType { get; set; } //Truck, SUV, Sedan, Convertible, etc
public string TransmissionType { get; set; }
public string InteriorColor { get; set; }
public string ExteriorColor { get; set; }
// Foreign Keys
public virtual int MakeId { get; set; }
// Navigation Properties
// Classes
[ForeignKey("MakeId")]
public virtual VehicleMake Make { get; set; }
// Collections
public virtual ICollection<ProductFeature> InteriorFeatures { get; set; }
public virtual ICollection<ProductFeature> ExteriorFeatures { get; set; }
public virtual ICollection<ProductFeature> SafetyFeatures { get; set; }
}
What would be the best way to set up inheritance so Vehicle, Parts, Supplies and any future sale item class [eg. clothing] can be added with not much fuss with redundant properties being coded?
The best way to start is to write down all of the properties for each item of interest currently.
So, any product will have a price and some unique id (sku number) and maybe a barcode and an image.
So, these could be the start of some parent class.
As you go through other products you may find commonalities.
If you need to start selling jeans, then look at what other clothing may be needed, as you may want to have material or style be listed.
But, don't try to design your classes so that they will able to handle anything.
Design for what you have right now, but make it flexible enough so you can add new properties if you need to. For example, right now I wouldn't add a qrcode image, but later it may be common enough to be added.
Is your question actually for class design or, ultimately, for database design?
I decided to combined Seasonal & Discontinued into Product and have Product inherit from Entity.
Many thanks to the Comments/Word police. I did not realize we had more editors on this site than answer helpers. So, this is how I will proceed:
public Product()
{
OrderDetails = new HashSet<OrderDetail>();
Photos = new HashSet<PhotoSource>();
ProductFeatures = new HashSet<ProductFeature>();
}
// Primitives
public string ProductName { get; set; }
public string InternalSKU { get; set; }
public string ModelNumber { get; set; }
public string Description { get; set; }
public int QtyPerUnit { get; set; }
public double UnitPrice { get; set; }
public int UnitsInStock { get; set; }
public int UnitsOnOrder { get; set; }
public int? ReOrderLevel { get; set; }
public string Warranty { get; set; }
// Primitives for Disontinues
public DateTime? DiscontinuedDate { get; set; }
public int? DiscontinuedDisount { get; set; }
// Primitives for Seasonal
public int? OffSeasonDiscount { get; set; }
public DateTime? SeasonStartDate { get; set; }
public DateTime? SeasonEndDate { get; set; }
public int? QtyLimitedTo { get; set; }
// Foreign Keys
public int SubCategoryID { get; set; }
public int VendorId { get; set; }
// Navigation Properties
// Classes
[ForeignKey("SubCategoryID")]
public virtual SubCategory SubCategory { get; set; }
[ForeignKey("VendorId")]
public virtual Vendor Vendor { get; set; }
// Collections
public ICollection<OrderDetail> OrderDetails { get; set; }
public virtual ICollection<PhotoSource> Photos { get; set; }
public virtual ICollection<ProductFeature> ProductFeatures { get; set; }
}
Some times "has a relationship" (composition) are easier to maintain than "is a relationship"(inheritance) as the application grows. Looks like this is one of those scenarios. You can set it up so that each of the entities has a product class. And you can use inversion of control to inject product into these entities.
This link explains the rationale in this article.
http://www.artima.com/designtechniques/compoinh4.html

Categories

Resources