How to Increase the performance of Entityframework ExecuteSqlCommand() with different ObjectContexts? - sql-server

We are Using Service fabric Actor application,in that we have multiple actors. if i want to Update 10 records each record acts as like different individual instance.so when we insert it will create new ObjectContext everytime. so we con't store cache data in context level. so my datamodel is like
public class StudentData {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public String StudentId { set; get; }
public string StudentName {get;set;}
public String StudentAge { set; get; }
public string StudentDob {get;set;}
public String StudentSTD { set; get; }
public string StudentEmail {get;set;}
public String StudentAddress { set; get; }
public string StudentReligion {get;set;}
}
And when we want to update 10 students 10 object instances will create. so for every instance it will call the below method. so below method will call 10 times as different instance id.
public async Update(){
using(var context = new DatabaseContext()){
context.InfoObjectDatas.Attach(studentObj);
context.Entry(studentObj).State = System.Data.Entity.EntityState.Modified;
await context.SaveChangesAsync();
} }

We are Using Service fabric Actor application,in that we have multiple actors. if i want to insert 10 records each record acts as like different individual instance.so when we insert it will create new ObjectContext everytime. so we con't store cache data in context level. so my datamodel is like
public class StudentData {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public String StudentId { set; get; }
public string StudentName {get;set;}
public String StudentAge { set; get; }
public string StudentDob {get;set;}
public String StudentSTD { set; get; }
public string StudentEmail {get;set;}
public String StudentAddress { set; get; }
public string StudentReligion {get;set;}
}
And when we want to insert 10 students 10 object instances will create. so for every instance it will call . so i am using like for loop for 10 objects.
public async insert(){
using(var context = new DatabaseContext()){
Student st=new Student();
context.StudentData.Add(st);
context.StudentData.SaveChanges();
} }

Related

Advanced NoSQL Query (RavenDB)

I'm trying to run a query that gets all of my references, but it isn't working.
What I have right now is
from UserGroups
where Id="ActionGroup"
select Accomplishments.ID, Accomplishments.Accomplish
But I need only the Accomplishments.Accomplish that belong in my other collection ActivityAccomplishments and these are nested in another object.
To be exact, I'm trying to figure out how to query the UserGroups collection and only look at the one with id="ActionGroup". After that I need all of the Accomplishments.Accomplish strings within the UserGroup list to be filtered out if they don't match a id in ActivityAccomplishment.
Basically, in the UserGroup I'm looking at it's List Accomplishments needs to filter out all strings within the Acc class that don't match an Id in ActivityAccomplishments. Can someone please help me.
Here are the classes I'm using.
public class UserGroups
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Acc> Accomplishments { get; set; }
}
public class Acc
{
public string Id { get; set; }
public List<string> Accomplish { get; set; }
}
public class ActivityAccomplishments
{
public string Id { get; set; }
}
try this:
from UserGroups
where Id = "ActionGroup" AND Accomplishments[].Accomplish != "theIdYouDontWant"
select Accomplishments[].Accomplish as AccomplishStringsList
(not necessary to add the 'as AccomplishStringsList' - it is just a name for the results)

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)

Does Web Api support two and multi dimensional arrays for serialization and parameter binding?

I know that web services with Web Api can handle all the serialization and parameter (or data) binding tasks automatically for one dimensional arrays.
For example, a model class like the following one can be converted to JSON/XML and bound to a parameter of type Person automatically by the web api framework:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double[] Notes { get; set; } // 1-dim array
}
What if the model class includes a two or multi dimensional array, like the following one? Can Web Api also handle such arrays automatically, or does the user need to write some extra code for serialization and data binding?
public class Person2
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public double[,] NoteMatrix { get; set; } // 2-dim array
}

Resources