How to map the relationship between these 2 generated classes? - vb.net

I used to EF Code First From Database to generate classes based on an existing database. I need to map the relationship between these 2 classes.I suspect I have to set a foreign key but don't what to set the foreign key too.
Partial Public Class be_Posts
<Key>
Public Property PostRowID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
<StringLength(255)>
Public Property Title As String
Public Property Description As String
Public Property PostContent As String
Public Property DateCreated As Date?
Public Property DateModified As Date?
<StringLength(50)>
Public Property Author As String
Public Property IsPublished As Boolean?
Public Property IsCommentEnabled As Boolean?
Public Property Raters As Integer?
Public Property Rating As Single?
<StringLength(255)>
Public Property Slug As String
Public Property IsDeleted As Boolean
End Class
Partial Public Class be_PostTag
<Key>
Public Property PostTagID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
Public Property PostRowID As Integer
<StringLength(50)>
Public Property Tag As String
End Class

Seems straight forward, but i may be missing something. haven't coded in VB in a while so forgive any typos.
You'll want to add a Foreign Key relationship on the PostRowID values on your two tables.
I believe it should look something like this when you're done.
Partial Public Class be_Posts
<Key>
Public Property PostRowID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
<StringLength(255)>
Public Property Title As String
Public Property Description As String
Public Property PostContent As String
Public Property DateCreated As Date?
Public Property DateModified As Date?
<StringLength(50)>
Public Property Author As String
Public Property IsPublished As Boolean?
Public Property IsCommentEnabled As Boolean?
Public Property Raters As Integer?
Public Property Rating As Single?
<StringLength(255)>
Public Property Slug As String
Public Property IsDeleted As Boolean
End Class
Partial Public Class be_PostTag
<Key>
Public Property PostTagID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
Public Property PostRowID As Integer
<StringLength(50)>
Public Property Tag As String
<ForeignKey("PostRowID")>
Public Property PostRow As be_Posts
End Class
Here is an article that references a simple example of defining this FK relationship with Code First, the examples are in C# but should translate fairly well.

Related

How to deserialize a large JSON with multiple tags, received in an API call

I am receiving a large block of JSON information from an API call.
This example lists 2 of the items received; normally, this list will contain hundreds.
The JSON received is:
{
"data":[
{
"id":"2324682","type":"organizations","attributes":
{
"psa-integration":null,
"name":"Client1",
"alert":null,
"description":null,
"organization-type-id":null,
"organization-type-name":null,
"organization-status-id":null,
"organization-status-name":null,
"my-glue-account-id":null,
"primary":true,
"quick-notes":null,
"short-name":"P",
"created-at":"2017-09-25T17:21:26.000Z",
"updated-at":"2018-02-09T17:25:34.000Z",
"my-glue-account-status":null,
"logo":"client1.jpg"
},"relationships":{"adapters-resources":{"data":[]}}},
{
"id":"2388378","type":"organizations","attributes":
{
"psa-integration":null,
"name":"Client2",
"alert":null,
"description":null,
"organization-type-id":53460,
"organization-type-name":"E123",
"organization-status-id":17054,
"organization-status-name":"Active",
"my-glue-account-id":null,
"primary":false,
"quick-notes":null,
"short-name":null,
"created-at":"2017-10-16T19:36:30.000Z",
"updated-at":"2018-08-21T19:06:47.000Z",
"my-glue-account-status":null,
"logo":"client2.jpg"
},"relationships":{"adapters-resources":{"data":[]}}}],
"meta":
{
"current-page":1,
"next-page":2,
"prev-page":null,
"total-pages":16,
"total-count":31,
"filters":
{
"id":{"permitted-values":[]},
"name":{"permitted-values":[]},
"organization-type-id":{"permitted-values":
[{"value":39230,"data":{"name":"client"}},{"value":39231,"data": {"name":"CLient2"}},{"value":39232,"data":{"name":"Internal"}}, {"value":39233,"data":{"name":"Other"}},{"value":39234,"data":{"name":"Partner"}},{"value":39235,"data":{"name":"Prospect"}},{"value":39236,"data":{"name":"Vendor"}},{"value":53460,"data":{"name":"newname"}}]},"organization-status-id":{"permitted-values":[{"value":17054,"data":{"name":"Active"}},{"value":17055,"data":{"name":"Inactive"}}]},"created-at":{"permitted-values":[]},"updated-at":{"permitted-values":[]},"my-glue-account-id":{"permitted-values":[]}}},"links":{"self":"https://api.itglue.com/organizations?page%5Bnumber%5D=1\u0026page%5Bsize%5D=2\u0026sort=id","next":"https://api.itglue.com/organizations?page%5Bnumber%5D=2\u0026page%5Bsize%5D=2\u0026sort=id","last":"https://api.itglue.com/organizations?page%5Bnumber%5D=16\u0026page%5Bsize%5D=2\u0026sort=id"}}
So far I have tried creating classes for the data, but it seems to give me no information.
In order to process the data, I had to change all the properties names, because many contain a dash:
For example, Created-at is now Created_at
I used jsonutils.com to create the VB.Net classes.
I load the JSON into a string called Change_String.
If anyone can explain what I'm doing wrong, I would appreciate it
Dim changed_string as string = "JSON INPUT _ instead of -"
Dim obj As Attributes
obj = JsonConvert.DeserializeObject(Of Attributes)(changed_string)
msgbox(obj.name)
Classes:
Public Class Attributes
Public Property psa_integration As Object
Public Property name As String
Public Property alert As Object
Public Property description As Object
Public Property organization_type_id As Integer?
Public Property organization_type_name As String
Public Property organization_status_id As Integer?
Public Property organization_status_name As String
Public Property my_glue_account_id As Object
Public Property primary As Boolean
Public Property quick_notes As String
Public Property short_name As String
Public Property created_at As String
Public Property updated_at As String
Public Property my_glue_account_status As Object
Public Property logo As String
End Class
Public Class AdaptersResources
Public Property data As Object()
End Class
Public Class Relationships
Public Property adapters_resources As AdaptersResources
End Class
Public Class Datum
Public Property id As String
Public Property type As String
Public Property attributes As Attributes
Public Property relationships As Relationships
End Class
Public Class Id
Public Property permitted_values As Object()
End Class
Public Class Name
Public Property permitted_values As Object()
End Class
Public Class Data
Public Property name As String
End Class
Public Class PermittedValue
Public Property value As Integer
Public Property data As Data
End Class
Public Class OrganizationTypeId
Public Property permitted_values As PermittedValue()
End Class
Public Class OrganizationStatusId
Public Property permitted_values As PermittedValue()
End Class
Public Class CreatedAt
Public Property permitted_values As Object()
End Class
Public Class UpdatedAt
Public Property permitted_values As Object()
End Class
Public Class MyGlueAccountId
Public Property permitted_values As Object()
End Class
Public Class Filters
Public Property id As Id
Public Property name As Name
Public Property organization_type_id As OrganizationTypeId
Public Property organization_status_id As OrganizationStatusId
Public Property created_at As CreatedAt
Public Property updated_at As UpdatedAt
Public Property my_glue_account_id As MyGlueAccountId
End Class
Public Class Meta
Public Property current_page As Integer
Public Property next_page As Object
Public Property prev_page As Object
Public Property total_pages As Integer
Public Property total_count As Integer
Public Property filters As Filters
End Class
Public Class Links
End Class
Public Class Organizations
Public Property data As Datum()
Public Property meta As Meta
Public Property links As Links
End Class
I gave it a second look.
The classes generated by the online service are not exactly spectacular.
This is a modified class structure that should provide the correct results.
I compacted everything in a single class named JSON_Organizations, which contains all the classes needed by the JSON deserializer.
All the properties are renamed using JSON Attributes, so the bad names are taken care of.
Sample converter:
Imports Newtonsoft.Json
Dim JSONObject As String = "[The JSON Object]"
Dim Organizations As JSON_Organizations.RootObject = New JSON_Organizations.RootObject
Organizations = JsonConvert.DeserializeObject(Of JSON_Organizations.RootObject)(JSONObject)
The JSON_Organizations container class:
Imports Newtonsoft.Json
Public Class JSON_Organizations
Public Class RootObject
<JsonProperty("data")>
Public Property Data As Datum()
<JsonProperty("meta")>
Public Property Meta As Meta
<JsonProperty("links")>
Public Property Links As Links
End Class
Public Class Datum
<JsonProperty("id")>
Public Property Id As String
<JsonProperty("type")>
Public Property Type As String
<JsonProperty("attributes")>
Public Property Attributes As Attributes
<JsonProperty("relationships")>
Public Property Relationships As Relationships
End Class
Public Class Attributes
<JsonProperty("psa-integration")>
Public Property PsaIntegration As Object
<JsonProperty("name")>
Public Property Name As String
<JsonProperty("alert")>
Public Property Alert As Object
<JsonProperty("description")>
Public Property Description As Object
<JsonProperty("organization-type-id")>
Public Property OrganizationTypeId As Integer?
<JsonProperty("organization-type-name")>
Public Property OrganizationTypeName As String
<JsonProperty("organization-status-id")>
Public Property OrganizationStatusId As Integer?
<JsonProperty("organization-status-name")>
Public Property OrganizationStatusName As String
<JsonProperty("my-glue-account-id")>
Public Property MyGlueAccountId As Object
<JsonProperty("primary")>
Public Property Primary As Boolean
<JsonProperty("quick-notes")>
Public Property QuickNotes As Object
<JsonProperty("short-name")>
Public Property ShortName As String
<JsonProperty("created-at")>
Public Property CreatedAt As DateTimeOffset
<JsonProperty("updated-at")>
Public Property UpdatedAt As DateTimeOffset
<JsonProperty("my-glue-account-status")>
Public Property MyGlueAccountStatus As Object
<JsonProperty("logo")>
Public Property Logo As String
End Class
Public Class Relationships
<JsonProperty("adapters-resources")>
Public Property AdaptersResources As AdaptersResources
End Class
Public Class AdaptersResources
<JsonProperty("data")>
Public Property Data As List(Of Object)
End Class
Public Class Links
<JsonProperty("self")>
Public Property Self As String
<JsonProperty("next")>
Public Property NextLink As String
<JsonProperty("last")>
Public Property Last As String
End Class
Public Class Meta
<JsonProperty("current-page")>
Public Property CurrentPage As Long
<JsonProperty("next-page")>
Public Property NextPage As Long
<JsonProperty("prev-page")>
Public Property PrevPage As Object
<JsonProperty("total-pages")>
Public Property TotalPages As Long
<JsonProperty("total-count")>
Public Property TotalCount As Long
<JsonProperty("filters")>
Public Property Filters As Filters
End Class
Public Class Filters
<JsonProperty("id")>
Public Property Id As CreatedAt
<JsonProperty("name")>
Public Property Name As CreatedAt
<JsonProperty("organization-type-id")>
Public Property OrganizationTypeId As CreatedAt
<JsonProperty("organization-status-id")>
Public Property OrganizationStatusId As CreatedAt
<JsonProperty("created-at")>
Public Property CreatedAt As CreatedAt
<JsonProperty("updated-at")>
Public Property UpdatedAt As CreatedAt
<JsonProperty("my-glue-account-id")>
Public Property MyGlueAccountId As CreatedAt
End Class
Public Class CreatedAt
<JsonProperty("permitted-values")>
Public Property PermittedValues As List(Of PermittedValue)
End Class
Public Class PermittedValue
<JsonProperty("value")>
Public Property Value As Integer
<JsonProperty("data")>
Public Property Data As Data
End Class
Public Class Data
<JsonProperty("name")>
Public Property Name As String
End Class
End Class

cant create a relationship between application user table and my tables

I'm using entity framework code first. The below keeps kicking out the unable to determine the principal end of an association. Basically I can't figure out how to get one to many relationships between the auto generated identity table and my own tables with code first.
public class applicationUser : Identity
{
public User user {get;set;}
}
//and then a dependant class called User
public class User
{
public in Id {get;set;}
public string Name {get;set;}
[ForeignKey("Identity")]
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}
You're using [ForeignKey()] wrong. The value of [ForeignKey()] should be the name of the property that contains the key of the referenced object.
You should also mark the property that contains the key in the referenced object as the private key with [Key]
Try this:
public class applicationUser : Identity
{
public int UserId {get; set;}
[ForeignKey("UserId")]
public User user {get;set;}
}
and then another class called User
public class User
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
public string IdentityId {get;set;}
public ApplicationUser Identity {get;set;}
public string UserPicLocation {get;set;}
}
I fixed it by setting the primary key as both the primary and foreign key in the dependant table

Automapper map registers (static data)

I have a DTO like this (note this is only a sample dto) :
public class SomeClassDTO
{
public guid Id {get;set;}
public string Name {get;set;}
public IList<Register1> Registers1 {get;set;}
public IList<Register2> Registers2 {get;set;}
}
and the class:
public class SomeClass
{
public guid Id {get;set;}
public string Name {get;set;}
}
I need something like this:
Mapper.CreateMap<SomeClass, SomeClassDTO>()
.ForMember(dto=>dto.Registers1, opt=>opt.MapFrom(src=>_session.Query<Register1>.ToList()))
.ForMember(dto=>dto.Registers2, opt=>opt.MapFrom(src=>_session.Query<Register2>.ToList()));
Where _session is an instance per request of the nhibernate session.
Ok, I need to get the collections in the dto populated from the values i get from DB using nhibernate session. (of course those collection has nothing to do with the class: SomeClass and they don't have any relations to it.
Dont know how to inject ISession to MapFrom or ResolveUsing.
The DI Container is Autofac, and the project type is WebApi2
Please Help

Several unique constrains on a single document in RavenDb

Let's say I have a class:
public class Person
{
public string Name{get;set;}
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
}
If I want to make the email unique I will use the unique constraint bundle.
But I want to make both the googleId and the facebookId properties as a single unique constraint side by side with the email constraint (while non of them is the id).
Is it possible?
Use the UniqueConstraints bundle:
public class Person
{
public string Name {get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string GoogleAndFacebookIds { get;set; }
}
Just make sure you update GoogleAndFacebookIds everytime you update either GoogleId or FacebookId. I was doing this so much I ended up using a simple interface on all my classes that did this sort of thing:
public interface ICombinedConstraints
{
string UniqueId { get; set; }
void UpdateConstraints();
}
So,
public class Person : ICombinedConstraints
{
public string Name{get;set;}
[UniqueConstraint]
public string Email {get;set;}
public string GoogleId {get;set;}
public string FacebookId {get;set;}
[UniqueConstraint]
public string UniqueId { get; set; }
public void UpdateConstraints()
{
UniqueId = GoogleId + FacebookId;
}
}
You cannot do that. Raven doesn't provide any possibility to enforce unique constraints on the property. If you want to do that you need separate sets of documents (see link below)
Unique constraints in Raven
Update:
It seems that you can also try to use bundles to implement unique constraints in the object.
Bundle: Unique constraints

PetaPoco mapping properties within properties

I am new to PetaPoco and initially I was liking it but then hit a wall which I simply dont know how to search for.
I have a object which needs to set a property within one of its properties, ie Job.Min.BaseValue. The source of this data is "min_mb".
So basically my object is not a direct mapping of the source table
public class Usage
{
public Decimal BaseValue {get;set;}
public Decimal BaseScale {get;set;}
public Decimal BaseUnit {get;set;}
}
[PetaPoco.TableName("data")]
[PetaPoco.PrimaryKey("date, client_name")]
[PetaPoco.ExplicitColumns]
public class Job
{
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public CommvaultJob() { Min = new Usage() { BaseScale=1024, BaseUnit="MB" }; }
}
I think you're just missing the extra type when you call Fetch or Query. This worked for me :
Calling PetaPoco :
var allData = _db.Fetch<TestJobPoco,Usage>("select * from dataTEST");
return View( allData);
The pocos :
[PetaPoco.ExplicitColumns]
public class Usage
{
public Usage()
{
BaseScale=1024;
BaseUnit="MB";
}
[PetaPoco.Column("base_value")]
public Decimal BaseValue {get;set;}
[PetaPoco.Ignore]
public Decimal BaseScale {get;set;}
[PetaPoco.Ignore]
public string BaseUnit {get;set;}
}
[PetaPoco.TableName("dataTEST")]
[PetaPoco.PrimaryKey("id")]
[PetaPoco.ExplicitColumns]
public class TestJobPoco
{
[PetaPoco.Column("id")]
public int Id {get;set;}
[PetaPoco.Column("date")]
public DateTime Date {get;set;}
[PetaPoco.Column("client_name")]
public String ClientName {get;set;}
public Usage Min {get;set;}
public TestJobPoco()
{
//Min = new Usage() { BaseScale=1024, BaseUnit="MB" };
}
}
My test database has an id, date, client_name and base_value columns. The primary key is id so it's slightly different than yours but this shouldn't change the way the poco mapping happens.
If your objects do not map with the table structure, an ORM can't help much.
You will need to do the mapping manually or made new shadow properties that copy the values of the other fields, but this added complexity will defeat the purpose of an ORM.