I have the following xml file
<Questions>
<QuestionId=1>
Which is a reserved word in the Java programming language?
<Option1>
method
</Option1>
<Option2>
native
</Option2>
<Option3>
subclass
</Option3>
<Option4>
reference
</Option4>
<Answer>
reference
</Answer>
</Question>
</Questions>
I need to create a table in the database where the value of Question becomes the column name and the Answers come in the row using C# and ASP.NET
Any help would be appreciated.
This is the format expected.
|A_ID||Which is a reserved word in the Java programming language?||Q2 |
________________________________________________________________________
|01 ||Reference ||A2 |
If I understand correct, you need 2 tables. 1 for the questions, and 1 for the answers. For simplicity I'm thinking of:
public class Question
{
public int Id { get; set; }
public string Question { get; set; }
//relations
public List<Answer> Answers { get; set; }
}
public class Answer
{
public int Id { get; set; }
public string Answer { get; set; }
public bool Correct { get; set; }
public int QuestionId { get; set; }
//relations
public Question Question { get; set; }
}
Then with entityframework you can create migrations and your context and create your questions etc.
Makes any sense?
if your new to entity framework: check out: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db
Related
I am learning EF Core and having some trouble understanding the One to Many Setup, particularly the Many part. I have googled and searched stack overflow but can't seem to find an obvious answer to my question.
I am using SQL Server community and a Code First approach. I have the following two entities, and can I insert a Person record into the database with the Foreign Key without issue. My question is when/where/how does the Persons List in Nationality get populated, as it doesn't seem to do it automatically. Is this something I can set up in EF, or do I need to populate it manually?
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public int? NationalityId { get; set; }
public virtual Nationality Nationality { get; set; }
}
public class Nationality
{
public int ID { get; set; }
public string Name { get; set; }
public virtual List<Person> Persons { get; set; }
}
This question already has answers here:
Create code first, many to many, with additional fields in association table
(7 answers)
Closed 4 years ago.
The only question that I found most similar to my question is here, But It didn't answer my question. I have this model:
public class Profile
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted {get; set; }
public virtual ICollection<Program> AllowedPrograms { get; set; }
public virtual ICollection<Program> DisAllowedPrograms { get; set; }
}
and this :
public class Program
{
public int Id { get; set; }
public string DisplayName { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted {get; set; }
public virtual ICollection<Profile> AllowedProfiles { get; set; }
public virtual ICollection<Profile> DisAllowedProfiles { get; set; }
}
If I implement soft delete by help of IsDeleted field, What happens to entries in ProfileProgram table ? Are they deleted too implicitly? (There is two many-to-many relationships in this model I guess ). If I use entity framework filters which is located in here.
Or should I create the intermediate table by myself and add IsDeleted field to that? Also this approach seems to change my code a lot which I am really looking for alternate method.
Yes the ProfileProgram table isn't automatically deleted. Yes you should create the IsDeleted table (not field).
I feel like this is going to be very obvious to many of you, but my research led me nowhere.
I'm trying to build a class that will create objects with a list of properties like this one:
public class MyObject
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public List<OpenHours> OpenHours { get; set; }
public Filter Filters { get; set; }
}
I want the OpenHours object to store a list of daily hours so that they are accessible by doing MyObject.OpenHours[index].property.
I'm currently getting the error that the OpenHours object does not have a defined key, but I actually don't want it to be in my database as a different entity, I just want it to store properties the same way that it would if I listed each of the weekDay's properties directly in the MyObject class.
Here is the OpenHours class:
public class OpenHours
{
public DayOfWeek Day { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
}
I want each of my "MyObject" objects to have unique OpenHours values, so I do not want to create an Id just for it.
Am I missing something in the syntax, or is it bad logic to do so?
Thanks in advance for your answers!
Edit: Someone answered and deleted his answer afterwards, so I can't mark it as the right answer, but the solution was to add [ComplexType] to my class :
[ComplexType]
public class OpenHours
{
public DayOfWeek Day { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
}
if each entry is going to be unique then make a compound primary key if you do not want to introduce an id. Make all the properties the primary key. This will be less efficient in DB terms than just creating an Id as the primary key though.
Someone answered and deleted his answer afterwards, so I can't mark it as the right answer, but the solution was to add [ComplexType] to my class :
[ComplexType]
public class OpenHours
{
public DayOfWeek Day { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
}
Links for additional information on Complex Types:
Associations in EF Code First: Part 2 – Complex Types
Entity Framework Complex Types - EF Designer
I'm currently developing a Forum (Question / Answer) based application.
Using C# ASP.net MVC and MongoDB for data storage.
I'm currently looking at the model.
I was thinking of having separate classes like this: (simplified)
public class Question
{
public string ID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public List<string> Tags { get; set; }
public DateTime DateCreated { get; set; }
public string ForumID { get; set; }
}
Answer
public class Answer
{
public string ID { get; set; }
public string QuestionID { get; set; }
public string Body { get; set; }
public DateTime DateCreated { get; set; }
}
My questions is:
How to handle the "replies"
Am I best of having (as in the model above) two separate "entities"
Or should I have a List of Answer in my Question model?
Some requirements are that i'll need to be able to display a count of answers etc...
With this being stored in a NoSQL db, I'm aware I should denormalize things, but how can I insert an answer, without retrieving the entire post? Is that sort of operation possible using NoRM with MongoDB?
Normally in MongoDB, you would embed the answers inside the question. 99% of the time you're going to query by Question, so you might as well get the Answers at the same time.
Some requirements are that i'll need to be able to display a count of answer...
If you're bringing back the answers with the questions, this is really easy. You'll have an array/list/collection with answers. So you'll just grab the length.
but how can I insert an answer, without retrieving the entire post
MongoDB supports an atomic "$push" operation. That means that you can add an item to an array without actually loading the document from the client. From the javascript shell, it would look like this:
db.questions.update( {_id : your_id}, { $push : { answers : your_answer_object } } );
So MongoDB is capable of this. You'll have to check with the NoRM drivers to ensure that they actually allow for this type of behavior (they're really missing something if they don't support $push).
Answer should be part of the question.
public class Question
{
public string ID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public List<string> Tags { get; set; }
public DateTime DateCreated { get; set; }
public string ForumID { get; set; }
public List<Answers> Answers { get; set; }
}
Because of the lack of joins document databases encourage you to store instances of the entire graph in one document.
I an developing a page where users will be able to add and modify existing content, its not a wiki per sé but sort of, like SO's editing abilities.
I am working with EF4 and the new Code First approach in the latest CTP, so what would be the best class design for this?
my current guess is something like this:
public class VersionableText
{
public int Id { get; set; }
public DateTime Date{ get; set; }
public String Text{ get; set; }
public virtual User User{ get; set; }
}
and then use it in my other entities, in a SO context it could be something like this
public class Question
{
public int Id {get; set;}
public virtual VersionableText Title {get; set;}
public virtual VersionableText Content{get; set;}
...
}
But I'm not really convinced by it.. since I am also going to have tags, ability to delete/undelete posts, rollback, etc. Do you know how to properly design classes that help me version the content properly?
Aim for simplicity
The main question that you need to ask yourself is Are you going to show all versions all the time or the latest version most of the time and all of them on request? Similar to here. Most of the time you only see the latest version.
If this is the same with our case I wouldn't care so much about these versions. But when you'd want to show them all on one page class design more or less depends on the way that you'd like to show it. Is it going to be showing changes and things like that.
I'd rather have a class like:
public class Question
{
public int Id { get; set; }
public QuestionStatus Status { get; set; }
}
public class QuestionHistory
{
public Question Question { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public User Author { get; set; }
public DateTime Created { get; set; }
public IList<Tag> Tags { get; set; }
}
And when I'd display all of them I'd just return a list of these ordered by LastChange. I've added tags list but I didn't add any of the other process-related properties related to question state. It hugely depends on the process sequence.