Link two entities based on unique value not ID - nhibernate

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...
}
}

Related

Get actual model type from dynamic type

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"));

How to deserialize object as single item list?

I need to make a service that allows both next jsons as request body
{
"id": 1,
...
"foo": null
}
and
{
"id": [1, 2, 3],
...
"foo": null
}
I tried making the model like this
public class MyModel
{
public List<int> Id { get; set; }
//...
public string Foo { get; set; }
}
which works for the second case, but not for the first one, as an int is not a list of ints.
What can I do to be able to parse both bodies correctly?
You could deserialize into a dynamic object instead.
dynamic result = JsonConvert.DeserializeObject<dynamic>(json);
Another approach I'm not sure would work is to make the Id property dynamic.
public class MyModel
{
public dynamic Id { get; set; }
public string Foo { get; set; }
}
You can create Models like the following(Model includes all the properties except id):
public class MyModel
{
public List<int> id { get; set; }
public Model model { get; set; }
}
public class Model
{
//...
public string foo { get; set; }
}
And ckeck id in requestbody,if id is int type,create a new list and add id to the list.
MyModel myModel = new MyModel();
myModel.model=JsonConvert.DeserializeObject<Model>(jsonResult);
var mydata = JsonConvert.DeserializeObject<JObject>(jsonResult);
if (!mydata["id"].ToString().Contains("["))
{
myModel.id = new List<int> { Convert.ToInt32(mydata["id"]) };
}
else {
myModel.id = JsonConvert.DeserializeObject<List<int>>(mydata["id"].ToString());
}
result:

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.

RavenDB issue storing object that inherits List<T>

I have a class that inherits from List, this class has some additional properties on it. When I store the parent document in RavenDB, the list items get stored but the additional properties do not.
The failing test below probably explains my issue better:
[TestFixture]
public class RDBIssueTests
{
private DocumentStore _documentStore;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_documentStore = new EmbeddableDocumentStore
{
RunInMemory = true,
UseEmbeddedHttpServer = true,
DataDirectory = "Data",
};
_documentStore.Initialize();
}
[Test]
public void StoreSimpleDataTest()
{
string id = "people/1";
string laptopName = "MacPro";
string personName = "Joe Bloggs";
string attrOne = "Screen";
string attrTwo = "Keyboard";
var person = new Person()
{
Id = id,
Name = personName,
Laptop = new Possession<string>()
};
person.Laptop.Name = laptopName;
person.Laptop.Add(attrOne);
person.Laptop.Add(attrTwo);
using (var session = _documentStore.OpenSession())
{
session.Store(person);
session.SaveChanges();
}
using (var session = _documentStore.OpenSession())
{
var loadedPerson = session.Load<Person>(id);
Assert.AreEqual(personName, loadedPerson.Name);
Assert.AreEqual(2, loadedPerson.Laptop.Count); // 2 items in the list
Assert.IsTrue(loadedPerson.Laptop.Contains(attrOne));
Assert.IsTrue(loadedPerson.Laptop.Contains(attrTwo));
Assert.AreEqual(laptopName, loadedPerson.Laptop.Name); // fails here - Person.Laptop.Name is not persisted in RBD
}
}
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public Possession<string> Laptop { get; set; }
}
public class Possession<TValueType> : PossessionAttribute<TValueType>
{
public string Name { get; set; } // RDB doesn't persist this value
}
public class PossessionAttribute<TValueType> : List<TValueType>
{
}
As you can see from the test, the string property 'Name' on the Possession class does not get saved.
Is there something I need to do to get this property to persist?
Many thanks!
JSON has no way of representing an object that is both a list and has properties.
That is why you cannot do that. You can have an object that contains a list property, which is a more natural way of going about it.

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();
}