binding an object graph with winforms - winforms

I want to know how many bindings I should have on a data entry form.
I have the following domain classes
public class SalesOrder
{
public virtual DocumentHeader DocumentHeader {get; set;}
// some other properties
}
public class DocumentHeader
{
public virtual Account Account {get; set;}
// some other properties
}
The context has
public DbSet<SalesOrder> SalesOrders { get; set; }
public DbSet<DocumentHeader> DocumentHeaders { get; set; }
public DbSet<Account> Accounts { get; set; }
My data entry screen displays fields from the sales order and document header, and it displays the account.
I have the following code to set up the bindingsource
var dset = this.Context.SalesOrders;
this.bindingSource.DataSource = dset.Local.ToBindingList();
How do I bind the DocumentHeader and Account properties on the data entry form?

Related

How can I use EF Core to display data from 3 different tables in my application

I have 3 different tables, Customer, Vehicle, Contact.
The customer and vehicle tables share the common key ReferenceID and the customer and contact table share the key ContactID.
I want to be able to use EF core to display data from all three tables and was wondering if there is a way to do it without using JOIN?
Below is my attempt but I get this error: SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
public class Customer
{
public string Name { get; set; }
[ForeignKey("Vehicle")]
public string ReferenceID { get; set; }
[ForeignKey("Contact")]
public string ContactID { get; set;}
public ICollection<Contact> Contact { get; set; }
public ICollection<Vehicle> Vehicle { get; set; }
}
public class Vehicle
{
public string ReferenceID { get; set;}
public string Make {get; set;}
}
public class Contact
{
public string ContactID {get; set;}
public string Name {get; set;}
}
You don't need the JOINS here, You already have the access to the other tables as Navigation Property. If you are not using the Lazy loading the properties Vehicle and Contact will load their data.
If the values didn't get load, you can load them as below.
var customers = _context.Customers.Include("Contact").Include("Vehicle");

Is it possible to have another object type as a property of SQL Server entity without using relation

I have smaller separate models I don't want to map to SQL Server database table. Is it possible to use them as properties of an object that's mapped to SQL Server table?
I know SQL Server is relational, but of a case where I don't to map these separate models to database table? I have done it several times with non relational databases like MongoDb.
Here is and example what I want to achieve
The DbContext class:
public class ApplicationDbContext: DbContext
{
....
public DbSet<User> Users {get; set;}
}
User class:
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public LoginProfile LoginProfile { get; set; }
}
LoginProfile class:
public class LoginProfile
{
public string Username { get; set; }
public string Password { get; set; }
}
In the above example User model class is the only one I want to have as table in my database. Is it possible to do that, and do things like
var password = user.LoginProfile.Password;
user.LoginProfile.username = "username";

How to to make entity relationships in WEB API database?

I'm making a task management tool using AngularJS for the frontend and ASP.NET WEB API 2 for the backend. I have two entities in the database, a "Task" and a "Type". Each task has one type associated. The user fills a form when he can create a new task, and he has to select a type for that task.
Here's the C# code:
// KBTM_Task.cs file
public class KBTM_Task
{
public int ID { get; set; }
public string TaskID { get; set; } // User defined ID
public string Title { get; set; }
public string Description { get; set; }
}
// KBTM_Type.cs file
public class KBTM_Type
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
So my question is: how do I "connect" the two in the database? What I mean is, let's say I want to POST data to the database. I have to make two POSTs, right? One for the Task and one for the Type, since they're two separate entities.
But since they're stored with two different IDs, how do I know that a certain task has a certain type? In other words, if I send a GET request to KBTM_Task, how do I get the type of that task?
Modify your KBTM_Task entity to include the Type Id and foreign key relationship
public class KBTM_Task
{
public int ID { get; set; }
public string TaskID { get; set; } // User defined ID
public string Title { get; set; }
public string Description { get; set; }
public int TypeID { get; set; }
[ForeignKey("TypeID")]
public virtual KBTM_Type Type { get; set; }
}
This way when you get the data from the API your task object will already include the key ("TypeID") that can be updated and related object ("Type") that you can access its properties (Name, Description, ...).
When you update TypeID on the client object (model) you can simply push the updated task object to the API using $http.put() to handle the database update.
1) Add foreign key using fluent api (or data annotation)
// KBTM_Task.cs file
public class KBTM_Task
{
public int ID { get; set; }
public string TaskID { get; set; } // User defined ID
public string Title { get; set; }
public string Description { get; set; }
public int KBTM_TypeID {get;set}
public virtual KBTM_Type {get; set}
}
// KBTM_Type.cs file
public class KBTM_Type
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public KBTM_Task KBTM_Task { get; set;}
}
Add the following in the class inheriting from DbContext
public class KbtmContext : DbContext
{
...
//public virtual DbSet<KBTM_Task> KbtmTasks {get; set;}
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure KBTM_TypeID as FK for KBTM_Task
modelBuilder.Entity<KBTM_Task>()
.HasRequired(k => k.KBTM_Type)
.WithRequiredPrincipal(ad => ad.KBTM_Task);
}
}
2) If exposing the entity class in API response or request then you need to exclude navigation property from being serialized.
// KBTM_Task.cs file
public class KBTM_Task
{
...
[JsonIgnore]
public virtual KBTM_Type Type { get; set; }
}
To use the [JsonIgnore] atttribute use Install-Package Newtonsoft.Json in package manager console.(One of the popular solutions to manage serialization)

How to return hierarchical graph with RIA services

I would like a DomainService as follows:
[EnableClientAccess]
public class FamilyService : DomainService
{
public IQueryable<Person> GetPeople() // for the time being fake out a set of people
{
var people = new[]{ new Person(), ... };
return people.AsQueryable();
}
}
The Person class should be simple enough:
public class Person
{
[Key]
public Guid Id { get; set; }
public Person Parent { get; set; }
public String Name { get; set; }
public Person[] Children { get; set; }
}
In the Silverlight client, the Person class that is generated does not include the Parent or Children properties. What am I missing?
(Perhaps I should point out that while this is obviously a mock example, I am planning on using a loosely coupled approach, that this mimics. In fact most of my data doesn't reside in a database at all A significant majority of the RIA samples use an end-to-end parity of model from the database to the UI, which is not an option in my case.)
Try adding the [Include] attribute to the Parent & Children properties.
public class Person
{
[Key]
public Guid Id { get; set; }
[Include]
public Person Parent { get; set; }
public String Name { get; set; }
[Include]
public Person[] Children { get; set; }
}
If the Person class is coming out of the DB (in your real world app) and you are using EF, then you need to force them to be loaded eagerly, using the .Include() method.

How to add foreign key information to code first POCO to comply with RIA Services

My two classes are as follows
public class Client
{
public Guid Id { get; set; }
public String Name{ get; set; }
public virtual ICollection<Product> Products{ get; set; }
}
public class Product
{
public Guid Id { get; set; }
public String Model { get; set; }
public String Version { get; set; }
[ForeignKey("Client_Id")]
public virtual Client Client { get; set; }
[ForeignKey("Client")]
public Guid? Client_Id { get; set; }
}
And the DbContext class is this:
public class ClientContext : DbContext
{
public DbSet<Client> Clients { get; set; }
public DbSet<Product> Products { get; set; }
}
I have confirmed that this model works with the associated database for all the usual CRUD stuff.
I have created a RIA DoaminService class on the web site as so:
[EnableClientAccess]
public class ClientDomainService : DbDomainService<ClientContext>
{
public IQueryable<DomainModel.Product> GetProducts()
{
return this.DbContext.Controllers;
}
public IQueryable<DomainModel.Client> GetClients()
{
return this.DbContext.Clients;
}
}
When I build the silverlight application I get the error:
Unable to retrieve association information for assocation .... Only
models that include foreign key information are supported.
I thought that the [ForeignKey] attributes added to the Client and Client_Id properties on the Product class would satisfy the "include foreign key information" requirement.
What am I missing?
Assuming your code is complete, you are missing the other side of the Foreign key relationship... the [Key] attributes on the primary keys.

Resources