Entity Framework - Assign parent object to object [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Let's suppose we have 2 tables:
Person
ID
Name
Nationality_ID (FK)
Nationality
ID
Name
With EF, in what contexts does it make sense and is correct to use each of the options below to add a nationality to a person? What is the different between them? What is the faster and the slower?
Option 1:
TheNationality.persons.Add(ThePerson);
Option 2:
ThePerson.nationality_id = TheNationality.id;
Option 3:
ThePerson.nationality = TheNationality;

If Person is the root of your aggregate and the focus of your application, most likely option 2 and option 3 make sense. Of those, option 3 is the more useful if you need to do additional domain logic based upon information in your nationality. Option 1 makes sense if the focus of your application is about nationality.
None of these methods is mutually exclusive. If you query and manipulate the objects from both perspectives, you can use options 1/3 or 1/2.

The resulting insert/update would be the same in all cases. Unless it is necessary to get TheNationality entity for some other reason, you could skip the read to obtain that and just assign the ID if you have it:
ThePerson.nationality_id = someNationalityIDVariable;

Related

Enum vs Static class in database table [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 months ago.
Improve this question
I have to store in database
[Column 1], [Column 2-start datetime]
Version1_1, 01-01-2011
Version1_2, 01-01-2011
Version1_3, 01-01-2011
…
I have a class with two fields: the version name and the datetime (+id).
The version name represents the first column. It can be an enum or a static class with constants.
Regarding almost any scenario I understand enums are better. But then, The DB will store integers in the first column instead of string values. Which gives me a feeling of uncertainty.
Are the enums still the best option in this scenario? I don't see disadvantages in lacking clean string values in [Column 1] in database.
If you store the values as integers in the database, you have several advantages:
Less storage space required
Easier querying without taking string comparisons into account
Better query performance because integer comparisons are much faster than string comparisons
A disadvantage on the database side is of course the reduced readability, but considering the advantages, I'd prefer integers in the database.
On the C# side, enums let you have the best of both worlds: integers inside and at the same time text identifiers when working with the values.

best method to if/else potentially large condition [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Working on an MVC application. I have hundreds of users. Currently I'm trying to give some of our top users (maybe 10) a discount if they log-in through their assigned work-place e-mails. The price break is shown in the Search result, Product page, Shopping Cart, and Checkout page. I had to work on this quickly to hack it for the holiday season, so currently the way I am doing this is something like this:
ProductDetails.aspx
if (user == "at#at.com") {
Product. Price * 20
}
else {
Product.Price
}
As you can see, this works for now, however I'd have to do this for all 4 pages, and as our discounted users increase, this may become too long and mundane. I'm looking at a way to go around having such a long if/else statement, and was wondering if it makes sense to use a stored procedure instead or a method?
I would suggest to add a discount field to the user table or any appropriate table in the database and save the discount info there. You can also create a new table that holds the discount information tied up to the different users and manage to return the discount value instead of going through an if/else statement.

Calendar with database SQL Server [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to programming an schedule in C# where I want to show all my tasks, for example:
01-20-2018 at 3pm meeting with boss. And have a question:
Should I create a table "task" where i save the date my id and boss id?
Would it overload the dB?
I need some tips, thank you!
How many interlocutors (bosses) will you have ? Certainly more than one.
I suggest you should have one table for each purpose :
A task_table for tasks with an ID_Task as primary key, Detail field, delay field, status field and son on.
A target_table for the targets which is/are involved in the task with an ID_target (primary key) with a name field, first name field, company field, observation field and whatever field you need.
And you link this two tables in an another one :
Rendezvous_table, with Id_rendezvous (primary key), datetimestamp field, Id_Task field, Id_target field and observation_field for observation on rendezvous.
This can be a good start. Beware the relations one to multiple, one to one, multiple to multiple. Multiple task for a same rendez-vous, only one interlocutor for one rendez-vous or multiple...
Hope it helps.

what's the best way to translate a data set to a CLR type [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Working on a legacy application where Data Access repository always returns a data set. Changing to any ORM frameworks is not an option for me at this point, looking for best options to map result set to a CLR type
I know of 2 easy ways to do this.
1 - Use Dapper.NET
const string query = "SELECT * FROM Users";
return connection.Query<User>(query);
With Dapper you don't even have to worry about getting the DataTable, just query the SqlConnection and get your type back.
2 - Use Automapper:
List<User> users = AutoMapper.Mapper.DynamicMap<IDataReader, List<User>>(
sourceDataTable.CreateDataReader());
Automapper can map your DataTable to your type.
Both methods require your class to have property names that match the column names in your source data.
I think they are both available as NuGet packages

what is best way to prepare C# objects in separate methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is better method to use for object preparation logic:
a) with return value
List<Users> users = LoadUsers();
users = PrepareUsers(users);
b) or with void type
List<Users> users = LoadUsers();
PrepareUsers(users)
Are you setting properties on existing User objects or are you creating new ones?
If you're simply changing existing objects, then there's no reason why you'd want to return them, it's redundant. Worse, it's misleading - the client will think his objects were left untouched and that you're creating new objects when in fact you're not.
If you're creating new ones, well then, you obviously need to return them.
Alternative b. since you are working with the same user objects, there is no reason to reassign the variable.

Categories