Get actual model type from dynamic type - asp.net-core

I have three tables in my database (Student, Course, All). When I add any data in Student & Course table, the data also gets added in All table.
So, I'm using dynamic type data to pass Student & Course type data to get saved in All table.
public IActionResult Add(Student model)
{ ...
bool studentData = _studentService.Add(model); //saving in Student table
bool allData= _allService.AddAll(model); // passing 'Student' type data
...
}
In the All service, I've a function like-
public bool AddAll(dynamic model) // 'Student' type data passed & received as dynamic
{...}
Now, I need the details about Student model type (table name, total data found etc.).
Is there any way to get it? And is it possible to get the Student or Course model type info if I use dynamic data type?
Any suggestion or help will be much appreciated :) Thanks in advance!

If you want to get model type,you can use model.GetType(),and you can use model.GetType().Name to get model type Name,here is a demo:
Model:
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
}
Action:
public void Add(Student model)
{
//Student s = new Student { StudentId=1, StudentName="s1" };
Course c = new Course { CourseId=1,CourseName="c1" };
bool allData = AddAll(c);
}
public bool AddAll(dynamic model)
{
var type = model.GetType();
var typeName = type.Name;
return true;
}
result:

Is this example demonstrate what you trying to do ?
// Some dummy classes
public class Student
{
public string Name { get; set; }
public Student(string name)
{
Name = name;
}
}
public class Course
{
public string Title { get; set; }
public Course(string title)
{
Title = title;
}
}
// put the method somewhere
private string TypeCasting(dynamic myContent)
{
if (myContent is Student littleStudent)
{
return littleStudent.Name;
}
if (myContent is Course someLovelyCourse)
{
return someLovelyCourse.Title;
}
return string.Empty;
}
// Example using
var student = TypeCasting(new Student("fossil"));
var course = TypeCasting(new Course("type casting"));

Related

MVC An entity object cannot be referenced by multiple instances of IEntityChangeTracker

I've got a model that represents a joint table (with payload) in my database:
public class UserHasCar
{
// Foreign keys
[Key, Column(Order = 0)]
public string ApplicationUserId { get; set; }
[Key, Column(Order = 1)]
public int CarId { get; set; }
// Navigation properties
[Required]
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public virtual Car Car{ get; set; }
// Additional fields
public int YearsRidden { get; set; }
}
public class Car
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<UserHasCar> UserHasCars { get; set; }
}
public class ApplicationUser : IdentityUser
{
public int BirthYear{ get; set; }
public virtual ICollection<UserHasCar> UserHasCars { get; set; }
}
I have a form that includes multiple select boxes, and upon submitting I want to clear out all records related to that user who submitted the form in the UserHasCar table and replace them with the new updated information. I'm getting a An entity object cannot be referenced by multiple instances of IEntityChangeTracker. because I am doing something wrong, but I don't see where I am using more than one context. This code happens in my controller:
public ApplicationUser GetCurrentUser()
{
return UserManager.FindById(User.Identity.GetUserId());
}
public string GetUserId()
{
string id = User.Identity.GetUserId();
var user = UserManager.FindById(id);
return user.Id;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ManageCars(FormCollection form)
{
string id = GetUserId();
// Remove cars records with my id from database
var queryCars = (from m in db.UserHasCars where m.ApplicationUserId == id select m).ToList();
foreach (var record in queryCars )
{
// Could the problem be here?
db.UserHasCars.Remove(record)
}
// Add user-submitted cars to the database
string carval = form["Cars[0]"];
Car car = (from m in db.Cars where m.Name == carval select m).First();
int carid = car.ID;
// I get the abovementioned title error here
db.UserHasCars.Add(
new UserHasCar()
{
ApplicationUser = GetCurrentUser(),
ApplicationUserId = id,
Car = car,
CarId = carid,
YearsRidden = 0
}
);
db.SaveChanges();
}
I've seen many SO posts, but can't seem the problem as why my code doesn't want to save the new database entries.
EDIT
The solution was to remove the call to get the user and replace it with a query. Why? I was making database conflict errors by having both types of calls (database and DataManager calls in the same controller action). I ended up using a modified GetUser() function instead of GetCurrentUser()
Code:
public ApplicationUser GetUser()
{
// As opposed to:
// UserManager.FindById(User.Identity.GetUserId())
// We make a database call to grab our user instead
// So we don't get database context conflicts by using UserManager
string id = GetUserId();
return db.Users.Where(m => m.Id == id).First();
}
public string GetUserId()
{
return User.Identity.GetUserId();
}
// snip
// in ManageCars(FormCollection form)
ApplicationUser user = GetUser();
// snip
var newRow = db.UserHasCars.Create();
newRow.ApplicationUser = user;
// snip
db.UserHasCars.Add(newRow);
Try removing this line:
ApplicationUser = GetCurrentUser(),
from your object instantiation when adding.
Entity populates this object automatically once you set the foreign key ApplicationUserId. If UserManager.FindById(User.Identity.GetUserId()) uses a different db context that's where your exception is coming from.
Also to save yourself further trouble down the line, you should always call db.SaveChanges() in between the two operations. If you're worried about the atomicity of the db operation, just wrap the whole thing in a Transaction.
And when adding new rows to a table, I usually prefer to use something like:
var newRow = db.SomeTable.Create();
newRow.SomeColumn1 = "something";
newRow.SomeColumn2 = 5;
db.SomeTable.Add(newRow);
db.SaveChanges();
In order to delete entries from UserHasCars you need to change their EntityState to Deleted.
Example:
foreach (var record in queryCars )
{
db.ObjectStateManager.ChangeObjectState(record, EntityState.Deleted);
}
Hope this will fix your issue.

One-to-Many relationship with ORMLite

The only examples I can find addressing this sort of scenario are pretty old, and I'm wondering what the best way is to do this with the latest version of ORMLite...
Say I have two tables (simplified):
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
Patients can have multiple Insurance policies at different "levels" (primary, secondary, etc). I understand the concept of blobbing the insurance information as a Dictionary type object and adding it directly to the [Patient] POCO like this:
public class Patient
{
public Patient() {
this.Insurances = new Dictionary<string, Insurance>(); // "string" would be the Level, could be set as an Enum...
}
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
public Dictionary<string, Insurance> Insurances { get; set; }
}
public class Insurance
{
public string Policy { get; set; }
}
...but I need the insurance information to exist in the database as a separate table for use in reporting later.
I know I can join those tables in ORMLite, or create a joined View/Stored Proc in SQL to return the data, but it will obviously return multiple rows for the same Patient.
SELECT Pat.Name, Ins.Policy, Ins.Level
FROM Patient AS Pat JOIN
Insurance AS Ins ON Pat.PatientId = Ins.PatientId
(Result)
"Johnny","ABC123","Primary"
"Johnny","987CBA","Secondary"
How can I map that into a single JSON response object?
I'd like to be able to map a GET request to "/patients/1234" to return a JSON object like:
[{
"PatientId":"1234",
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}]
I don't have a lot of hope in this being do-able in a single query. Can it be done in two (one on the Patient table, and a second on the Insurance table)? How would the results of each query be added to the same response object in this nested fashion?
Thanks a ton for any help on this!
Update - 4/29/14
Here's where I'm at...In the "Patient" POCO, I have added the following:
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Ignore]
public List<Insurance> Insurances { get; set; } // ADDED
}
Then, when I want to return a patient with multiple Insurances, I do two queries:
var patientResult = dbConn.Select<Patient>("PatientId = " + request.PatientId);
List<Insurance> insurances = new List<Insurance>();
var insuranceResults = dbConn.Select<Insurance>("PatientId = " + patientResult[0].PatientId);
foreach (patientInsurance pi in insuranceResults)
{
insurances.Add(pi);
}
patientResult[0].Insurances = insurances;
patientResult[0].Message = "Success";
return patientResult;
This works! I get nice JSON with nested items for Insurances while maintaining separate related tables in the db.
What I don't like is that this object cannot be passed back and forth to the database. That is, I can't use the same nested object to automatically insert/update both the Patient and InsurancePolicy tables at the same time. If I remove the "[Ignore]" decorator, I get a field in the Patient table called "Insurances" of type varchar(max). No good, right?
I guess I'm going to need to write some additional code for my PUT/POST methods to extract the "Insurances" node from the JSON, iterate over it, and use each Insurance object to update the database? I'm just hoping I'm not re-inventing the wheel here or doing a ton more work than is necessary.
Comments would still be appreciated! Is Mythz on? :-) Thanks...
An alternate more succinct example:
public void Put(CreatePatient request)
{
var patient = new Patient
{
Name = request.Name,
Insurances = request.Insurances.Map(x =>
new Insurance { Policy = i.Policy, Level = i.Level })
};
db.Save<Patient>(patient, references:true);
}
References are here to save the day!
public class Patient
{
[Alias("PatientId")]
[Autoincrement]
public int Id { get; set; }
public string Name { get; set; }
[Reference]
public List<Insurance> Insurances { get; set; }
}
public class Insurance
{
[Alias("InsuranceId")]
[Autoincrement]
public int Id { get; set; }
[ForeignKey(typeof("Patient"))]
public int PatientId { get; set; }
public string Policy { get; set; }
public string Level { get; set; }
}
I can then take a JSON request with a nested "Insurance" array like this:
{
"Name":"Johnny",
"Insurances":[
{"Policy":"ABC123","Level":"Primary"},
{"Policy":"987CBA","Level":"Secondary"}
]
}
...to create a new record and save it like this:
public bool Put(CreatePatient request)
{
List<Insurance> insurances = new List<Insurance>();
foreach (Insurance i in request.Insurances)
{
insurances.Add(new Insurance
{
Policy = i.Policy,
Level = i.Level
});
}
var patient = new Patient
{
Name = request.Name,
Insurances = insurances
};
db.Save<Patient>(patient, references:true);
return true;
}
Bingo! I get the new Patient record, plus 2 new records in the Insurance table with correct foreign key references back to the PatientId that was just created. This is amazing!
First you should define a foreign collection in Patient class. (with get and set methods)
#ForeignCollectionField
private Collection<Insurance> insurances;
When you query for a patient, you can get its insurances by calling getInsurances method.
To convert all into a single json object with arrays inside you can use a json processor. I use Jackson (https://github.com/FasterXML/jackson) and it works very well. Below will give you json object as a string.
new ObjectMapper().writeValueAsString(patientObject);
To correctly map foreign fields you should define jackson references. In your patient class add a managed reference.
#ForeignCollectionField
#JsonManagedReference("InsurancePatient")
private Collection<Insurance> insurances;
In your insurance class add a back reference.
#JsonBackReference("InsurancePatient")
private Patient patient;
Update:
You can use Jackson to generate objects from json string then iterate and update/create database rows.
objectMapper.readValue(jsonString, Patient.class);

Conditional Restrictions in Fluent NHibernate using Query Object Pattern

I'm a complete noob to Fluent NHibernate, and I'm using the Query Object Pattern based on a recommendation. Which I'm also new to. I'll try to keep the code samples concise and helpful.
User class:
public class User {
public Guid ID { get; set; }
public string Name { get; set; }
}
Visibility:
public enum VisibilityType {
Anybody,
OwnersOnly,
Nobody
}
Car class:
public class Car {
public Guid ID { get; set; }
public VisibilityType Visibility { get; set; }
public ICollection<User> Owners { get; set; }
}
So I need to write a conditional restriction method for the query object. Return all cars that have VisibilityType.Public, but if a car has Visibility property value of VisibilityType.OwnersOnly, restrict the return to users who belong to that group.
Here's the current restriction method that I have working, but without the condition:
public class CarQueryObject
{
private User user { get; set; }
private const string OwnersProperty = "Owners";
private const string OwnersIDProperty = "Owners.ID";
public CarQueryObject RestrictToOwners()
{
// How do I add a conditional criteria here? Only restrict by owner
// if the QueryObject has VisibilityType.OwnersOnly? Note that it should
// *NOT* restrict VisibilityType.Anybody
CreateOwnersAlias();
Criteria.Add(Restrictions.Eq(OwnersIDProperty, user.Id));
return this;
}
public CarQueryObject JoinFetchOwned()
{
Criteria.SetFetchMode(OwnersProperty, FetchMode.Join);
return this;
}
public void CreateOwnersAlias()
{
Criteria.CreateAlias(OwnersProperty, OwnersProperty, JoinType.LeftOuterJoin);
JoinFetchOwned();
}
}
?_?
an idea to get shown cars
var carsShown = session.CreateCriteria<Car>()
.JoinAlias("Owner", "owner")
.Add(Expressions.Or(
Expression.Eq("Visibility", Visibility.Anybody),
Expression.Eq("Visibility", Visibility.OwnersOnly) && Expression.Eq("owner.Id", currentUser.Id)
))
.List<Car>();

ORMers,how are you going to deal with this in the ORM style?

We are modifying our post on stackoverflow.
And we only changed the tags part,removed tag1,tag2 and added tag3,tag4.
After pressing the Post Your Question button,these things should be done:
reduced the count column for tag1,tag2 by 1
delete the relation between the post and tag1,tag2
if tag3,tag4 already exists,increase the count column of the two by 1;otherwise,insert them to the tags table with count value 1
add the relation between the post and tag3,tag4
Let's take a deep breath and that's all!
I want to see which ORM can approach this most easily/performant no matter it's written in PHP/Java/C/.Net or any language else,because the ideas are similar across languages!
In DataObjects.Net it will look like this. There is now any mapping files because database schema is automatically generated by ORM, including auxiliary table for question-tag relation.
Tag class:
[HierachyRoot]
public class Tag : Entity
{
[Field, Key]
public int Id { get; private set; }
[Field(Length = 100, Indexed = true)]
public string Name { get; private set; }
[Field]
public int QuestionsCount { get; set; }
public Tag(string name)
{
Name = name;
}
}
Question class:
[HierachyRoot]
public class Question : Entity
{
[Field, Key]
public int Id { get; private set; }
[Field]
public EntitySet<Tag> Tags { get; private set; }
// Business methods (can be placed in separate service class)
public void AddTag(string name)
{
var tag = Query.All<Tag>().SingleOrDefault(t => t.Name == name);
if (tag==null)
tag = new Tag(name) { QuestionsCount = 1 }
else
tag.QuestionsCount++;
Tags.Add(tag);
}
public void RemoveTag(string name)
{
var tag = Query.All<Tag>.Single(t => t.Name == name);
tag.QuestionsCount--;
Tags.Remove(tag);
}
}
Application code:
using (Session.Open())
using (var transactionScope = Transaction.Open())
{
var question = Query.Single<Question>(questionId);
question.RemoveTag("tag1");
question.RemoveTag("tag2");
question.AddTag("tag3");
question.AddTag("tag4");
transactionScope.Complete();
}

Link two entities based on unique value not ID

I have the following database tables. Primary keys are ID and AnimalType.Type is a unique string.
Animal
- ID
- Name
- TypeID
AnimalType
- ID
- Type [Herbivore, Carnivore]
My classes are as follows.
public class Animal
{
public int ID { get; private set; }
public string Name { get; set; }
public AnimalType Type { get; set; }
}
public class AnimalType
{
private int ID { get; set; }
public string Type { get; set; }
}
How would I get the following code to work in NHibernate so that it references the same AnimalType of Herbivore?
var horse = new Animal() { Name = "Horse", Type = new AnimalType() { "Herbivore" }};
repository.SaveOrUpdate(horse);
var rabbit = new Animal() { Name = "Rabbit", Type = new AnimalType() { "Herbivore" } };
repository.SaveOrUpdate(rabbit);
UPDATE
Be nice if I could get NHibernate to perform this logic: http://andreas.scherbaum.la/blog/archives/11-Avoid-Unique-Key-violation.html
I'd investigate a few options.
1) If the data doesn't change frequently, can you make AnimalType an Enum instead of an Entity Object?
2) Instantiate an object of the Herbivore AnimalType using animalTypeRepository.FindByType("Herbivore") and pass that object into your new Animal.
3) Move the above logic into the animalRepository.SaveOrUpdate(animal) method so that you'd have...
public class AnimalRepository
{
public void SaveOrUpdate(Animal animal)
{
var animalType = animal.Type;
if (animalType.ID == 0)
{
animal.Type = animalTypeRepository.Find(animalType.Type);
}
// save or update animal...
}
}