How to map non-primitive members to bytes/JSON with NPoco? - mapping

Say I have an object:
class Person {
public int Id {get; set;}
public string Name {get; set;}
public Address HomeAddress {get; set;};
}
class Address {
public string Street {get; set;}
public string City {get; set;}
}
I want/need the above object to be mapped to a single column named Person which has a column named HomeAddress whose content is a JSON (or some custom binary serialization).
How do I tell NPoco Database object to use such mapping?

You would use the [SerializedColumn] attribute on the HomeAddress property of the Person class. This will serialize/deserialize json text that is stored in the column.

Related

Entity Framework using foreign key as only primary key in one to many

I am using Entity Framework code first to design the database.
I have 2 models with One to Many relationship. One "Foo" can have many "FooData" as follows -
public class Foo {
[Key]
public string serialNumber{get; set;}
public int someNumber {get; set;}
public string someName {get; set;}
// Many more properties
// Navigation Collection
public virtual ICollection<FooData> FooDatas{get; set;}
}
public class FooData{
[Key]
[ForeignKey("foo")]
public string SerialNum {get; set;}
public DateTime SomeTime {get; set;}
public byte[] SomeData {get; set;}
// Navigation property
public virtual Foo foo {get; set;}
}
When I try to add a controller for "Foo" in MVC, using "Foo" as a scaffolding model, it gives me this error - "Multiplicity is not valid in Role. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1".
I would appreciate some help here.
Thank you
Because of how you have it defined the same key is used in both so it is a 1-to-1 relationship. If you want a real one to many you will need to add/create another field and set them up as a composite key on the FooData table/entity.
public class Foo {
[Key]
public string serialNumber{get; set;}
public virtual ICollection<FooData> FooDatas{get; set;}
}
public class FooData {
[Key, Column(Order = 0),ForeignKey("foo")]
public string SerialNum {get; set;}
[Key,Column(Order=1)]
public int DataId {get;set;}
public virtual Foo foo {get; set;}
}

Multiple one-to-one relationships to same table

Which is the best way to model multiple one-to-one relationships to a same table (sqlite-net-extensions)? I am looking for a solution like this:
Class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
[ForeignKey(typeof(Organization ))]
public int ConsignerId {get; set;}
[OneToOne]
public Organization Consigner {get; set;}
[ForeignKey(typeof(Organization ))]
public int ConsigneeId {get; set;}
[OneToOne]
public Organization Consignee {get; set;}
}
Class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
}
Obviously, the above won't work.
I have considered other options too:
create a table (WayBillOrganization) that captures the role of organization:
Waybill -->> WayBillOrganization --> Organization
put the required inverse OneToMany properties in to the Organization.
Handle things manually (ie. store only primary keys in the WayBill class and load the Organizations separately).
Option 2. is something I want to avoid. Organizations are related to so many other classes (not in my example) and even in the case of a waybill, there's a few more relationships I didn't include into the example (carrier, cargo paying party, freight forwarder and so on). Besides, I'd rather use inverse properties only when I need to navigate (for example, I don't use organization to find waybills, so an inverse property is only an extra burden.)
Option 3. isn't that attractive either.
So the option 1. seems to be the way to go. But before going there, I'd like to know if the perfect-world solution in my example is indeed impossible.
So, my question is: Is there a way to model multiple one directional OneToOne relationships without explicitly declared inverse properties?
Your use case is supported in SQLite-Net Extensions. You only have to specify the foreign keys explicitly in the relationship attributes, because automatic discovery may not work as expected:
class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public int ConsignerId {get; set;}
[OneToOne("ConsignerId")]
public Organization Consigner {get; set;}
public int ConsigneeId {get; set;}
[OneToOne("ConsigneeId")]
public Organization Consignee {get; set;}
}
class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
}
Inverse relationships to the same class are also supported, but must be also declared explicitly at both ends:
class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public int ConsignerId {get; set;}
[OneToOne(foreignKey: "ConsignerId", inverseProperty: "ConsignerInverse")]
public Organization Consigner {get; set;}
public int ConsigneeId {get; set;}
[OneToOne(foreignKey: "ConsigneeId", inverseProperty: "ConsigneeInverse")]
public Organization Consignee {get; set;}
}
class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
[OneToOne(foreignKey: "ConsigneeId", inverseProperty: "Consignee")]
public WayBill ConsigneeInverse { get; set; }
[OneToOne(foreignKey: "ConsignerId", inverseProperty: "Consigner")]
public WayBill ConsignerInverse { get; set; }
}

validation filter for inherited property mvc model

I have a model imported from entity data framework
public class person
{
public string Name {get; set;}
}
and a viewmodel inherited from person
public class personviewmodel:person
{
public int Applied {get; set;}
}
I use personviewmodel as the model in my view. I would like to add error validation (data annotation) to 'Name' property without modifying the generated 'person' class. Can you please help get this?
You can implement validation as follows. But thing here you are not inheriting Person using person's object as is. Hope this will help you.
public class Person
{
[Required(ErrorMessage = "Message")]
public string Name {get; set;}
}
public class personviewmodel
{
public int Applied {get; set;}
public Person Person {get; set;}
}
or
public class Person
{
public string Name {get; set;}
}
public class Personviewmodel : Person
{
public int Applied {get; set;}
[Required(ErrorMessage="Name is required")]
public new string Name {get; set;}
}
or
public class Person
{
public virtual string Name {get; set;}
}
public class Personviewmodel : Person
{
public int Applied {get; set;}
[Required(ErrorMessage="Name is required")]
public override string Name {get; set;}
}

Entity framework code first - map class with List<> of another class to multiple tables?

I'm evaluating EF4 and have a pretty basic question...I think, that I can't seem to find an answer for..
take the following example:
public class Question
{
public string question {get;set;}
public string answer {get; set;}
}
public class Person
{
public string Id {get; set;}
public string Name {get; set;}
public List<Question> Questions {get; set;}
}
Then I have the following tables in the database
Person
(
id,
name
)
Question
(
id,
personId,
question,
answer,
)
Can I use the EF4 code first to map the Person class to the two tables, or do I ahve to restructure my POCO's first so the question class contains the id and personId - which is not something I would like to do.
Can I add something to the OnModelCreating to map the class as I need it to be mapped?
Thanks!
Ok here's what I've done for now - but it requires me having to restructure my question class...
public class Question
{
public int Id {get;set;} /* New */
public int PersonId {get;set;} /* New */
public string question {get;set;}
public string answer {get; set;}
public virtual Person PersonObj {get;set;}
}
public class Person
{
public string Id {get; set;}
public string Name {get; set;}
public List<Question> Questions {get; set;}
}
and added the following in the OnModelCreating event
modelBuilder.Entity<Person>().
HasMany(d => d.Questions).
WithRequired(c => c.Person).
HasForeignKey(c => c.PersonId).
WillCascadeOnDelete();
Not sure it's fully right...but seems to be working for now.

ASP.NET MVC Field naming convention for nested objects

I am using ASP.NET MVC 1.0.
I am creating a view to Create a new object of the following class
public class ClassOne
{
public string Name {get; set;}
public string Id {get; set; }
public CaptionItem Caption {get; set;}
}
public class CaptionItem
{
public string CaptionSet {get; set;}
public string Text {get; set;}
}
The view will have fields to input Name, Id and the Text of the caption. On submitting the form the ClassOne object is sent to the controller which is supposed to persist the object in the database.
The question is; what should be the naming conventions used so that I can send the object to the controller as
public ActionResult SaveObject(ClassOne objectToSave)
The challenge is the Caption object
you're right, to get to Text field you have to type Caption.Text :)

Resources