I'm trying to add a group Id to the database but my data model has type "Group" how can I achieve adding a GroupId without sending the whole object
Here is my Data model class
public class Tool : IObjectBase
{
public Group Group { get; set; } // My problem is here
public string Code { get; set; }
public string Name { get; set; }
public string Statut { get; set; }
public string StorageLocation { get; set; }
public string Description { get; set; }
public DateTime? CalibrationDate { get; set; }
public string InventoryNumber { get; set; }
public string SerialNumber { get; set; }
public string Contact { get; set; }
public string Image { get; set; }
public string StandardLoanPeriod { get; set; }
using Entity Framework core I was able to add this class as a table to the database and of course ef will make that group object as an GroupId in the table
So now in my angular application, I am trying to add a new Tool to the database using this Method
AddNewTool(form: NgForm) {
this.tool = {
Id : form.value.Id,
Code: form.value.Code,
Name: form.value.Name,
Description: form.value.Description,
Image: '',
Group: this.Group // Probleme is here
};
this.service.AddTool(this.tool).subscribe(
res => {
this.ToolToReload.emit(this.service.getTools());
this.toolRowInformation = null;
console.log(res);
form.resetForm();
} ,
err => {
console.log(err);
});
}
Now, whenever I add a new tool it works and the groupId get filled with that group but at the same time, that group added to the group table in the database.
I tried sending only the GroupId from angular but I get an error telling that the Tool is Expecting a Group object not a string.
My question is is it possible to add a tool with a groupId without sending the whole Group Object?
You need to have a GroupId property in your data model class as well as the group object.
This way, you can send the GroupId to and from the client and also access the Group navigation property in your data model class:
public int GroupId { get; set; }
Related
I'm trying to get data from SQLite in C# and put it into an array.
//class
public class DBStudentsInfo
{
public int uid { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public string[] ManyPoints { get; set; }
public string FinalPoint { get; set; }
public string Memo { get; set; }
public string Grade { get; set; }
}
I want to put the information from the DB into a class object. Example: DB status:
uid: 1, name: "ff", number: "1"...
// class object
DBStudentInfo information = new DBStudentInfo ();
info.Name = ...
What should I do?
You can use Entity Framework which is ORM (Object Relational Mapper).
https://learn.microsoft.com/en-us/ef/
if you have an existing database, you can use Database First Approach
https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/database-first-approach-in-entity-framework/
Am trying to Map an Anonymous object in auto mapper but am not getting how to do that. Please find my requirement below
Am getting some data from joining 2 tables with only one common column(Id). Am getting Anonymous type data from this query.
var query = (from _vdata in Table1
join entityFind in Table2 on _vdata.id equals entityFind.id
select new { entityFind.FamilyName, entityFind.LastLogin, entityFind.GivenName,
entityFind.Email, entityFind.EmailVerified, entityFind.Uuid, _vdata.Role,
_vdata.Payers, _vdata.Accounts, _vdata.ModifiedOn }).ToList();
Am getting Anonymous data from above query. I have some more list of data in another variable i need to add those data to this list with limited columns having 4-5 columns.
How to do mapping in this situation in AutoMapper or in any other technique
thanks
Since all anonymous types derived from System.Object, I found a solution (workaround) to add mapping from object to your destination type
//Allow to map anonymous types to concrete type
cfg.CreateMap(typeof(object), typeof(ExternalCandle),
MemberList.None);
But please note that for most scenarios this is not the correct solution
For example, if you want to map ORM types - go with this way: Queryable Extensions
I guess that Jimmy bogard won't recommend this solution because of the same reason that CreateMissingTypeMaps was removed from AutoMappers's API -https://github.com/AutoMapper/AutoMapper/issues/3063
So maybe in a future version of AutoMapper this code won't work (I am using AutoMapper 10.1.1 and it worked for me)
You cannot map anonymous type. To achieve the above functionality you can create a Model like below:
public class ResultantData
{
public string FamilyName { get; set; }
public string LastLogin { get; set; }
public string GivenName { get; set; }
public string Email { get; set; }
public string EmailVerified { get; set; }
public string Uuid { get; set; }
public string Role { get; set; }
public string Payers { get; set; }
public string Accounts { get; set; }
public string ModifiedOn { get; set; }
}
Then you can write the above query as below and return the IQueryable of the result:
var query = (from _vdata in Table1
join entityFind in Table2 on _vdata.id equals entityFind.id
select new ResultantData
{
entityFind.FamilyName,
entityFind.LastLogin,
entityFind.GivenName,
entityFind.Email,
entityFind.EmailVerified,
entityFind.Uuid,
_vdata.Role,
_vdata.Payers,
_vdata.Accounts,
_vdata.ModifiedOn
});
When you want to map this result to actual model then you can use ProjectTo method of Automapper as below:
var result = query.ProjectTo<ResultantDataModel>().ToList();
I have used below class as result model:
public class ResultantDataModel
{
public string FamilyName { get; set; }
public string LastLogin { get; set; }
public string GivenName { get; set; }
public string Email { get; set; }
public string EmailVerified { get; set; }
public string Uuid { get; set; }
public string Role { get; set; }
public string Payers { get; set; }
public string Accounts { get; set; }
public string ModifiedOn { get; set; }
}
I have an order object with its partial class containing changes history:
public class SomeOrder
{
public string SomeOrderNumber { get; set; }
public string Status { get; set; }
public IEnumerable<SomeOrderChangesHistory> ChangesHistory { get; set; }
}
public partial class SomeOrderChangesHistory
{
public string PropertyName { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
public DateTime DateTimeUtc { get; set; }
public string UserName { get; set; }
}
I'll be getting a list of SomeOrder and I want to filter out the orders if they are updated by api user.
The orders updated by api user will have one or more SomeOrderChangesHistory object in the ChangesHistory list with api user value in the UserName property (in SomeOrderChangesHistory object).
How do I accomplish this?
Based on #NetMage's excellent comment:
var ordersNotUpdatedByAPIUser = orders
.Where(o => !o.ChangeHistory.Any(ch => ch.UserName == "api user"))
.ToList()
It will filter out all the orders updated by api user which is exactly what I want.
I'm trying to get data in a suitable format for an api
What I would like is
Place
--Rating1
---RatingImage1.1
---RatingImage1.2
---UserName
---UserId
--Rating2
---RatingImage2.1
---RatingImage2.2
---UserName
---UserId
In a nutshell im trying to fetch a place, with its ratings(and rating images), with the names of the users who did the rating given the googlePlaceId
Tried this but it goes and does some circular fetching where once it fetches the user it then fetches the user rating and the response becomes massive
context.Places
.Include(x => x.Ratings.Select(y => y.User))
.Include(x => x.Ratings.Select(c => c.RatingImages))
.Single(x => x.GooglePlaceId == googlePlaceId);
I think projection or linq joins must be the way, but i havent had any success yet.
here are my POCOS
Place Poco
public class Place
{
public Place()
{
Ratings = new List<Rating>();
Favourites = new List<Favourite>();
}
public int Id { get; set; }
public string Name { get; set; }
public string GooglePlaceId { get; set; }
public ICollection<Rating> Ratings { get; set; }
public ICollection<Favourite> Favourites { get; set; }
}
Rating POCO
public class Rating
{
public Rating()
{
RatingImages = new List<RatingImage>();
}
public int Id { get; set; }
public float RatingValue { get; set; }
public string RatingComment { get; set; }
public int PlaceId { get; set; }
public Place Place { get; set; }
public string UserId { get; set; }
public AspNetUser User { get; set; }
public ICollection<RatingImage> RatingImages { get; set; }
}
User POCO
public partial class AspNetUser
{
public string UserName { get; set; }
public string Id { get; set; }
// the rest of the fields are omitted
}
Although you've omitted the definition of AspNetUser, I'm guessing it has a navigation property back to Ratings. Is this required anywhere else in your application? It won't affect the structure of your database, and removing it would allow your projection to work exactly as you've got it here. You'd still be able to display all ratings by a single user using a separate query - you've got to optimise for your most common scenario though.
I have a model that I'm loading into a table within a form. The records are retrieved from an Oracle DB using EF6 and loaded into the model.
I also want the user to be able to select records to delete from the database via a checkbox in each row in the form.
The function to retrieve the Attendees:
public List<WebinarAttendeesList> getAttendees(string webinarKey)
{
string connectionString = "Password=password;User Id=user;Data Source=Oracle";
List<WebinarAttendeesList> r = null;
using (webinarAttendeesListDbContext context = new webinarAttendeesListDbContext(connectionString))
{
var result = from w in context.WebinarAttendeesList
where w.webinarKey == webinarKey
orderby w.FirstPollCount, w.SecondPollCount
select w;
r = result.ToList();
}
return r;
}
Here is the model:
[Table("WEBINARATTENDEESLIST")]
public class WebinarAttendeesList {
[Key, Column("WAL_ID")]
public int wa_id { get; set; }
[Column("WAL_CLI_RID")]
public int ParticipantID { get; set; }
[Column("WAL_FULLNAME")]
public string FullName { get; set; }
[Column("WAL_EMAIL")]
public string Email { get; set; }
[Column("WAL_JOINTIME")]
public string JoinTime { get; set; }
[Column("WAL_TIMEINSESSION")]
public string TimeInSession { get; set; }
[Column("WAL_LEAVETIME")]
public string LeaveTime { get; set; }
[Column("WAL_FIRSTPOLLCOUNT")]
public int FirstPollCount { get; set; }
[Column("WAL_SECONDPOLLCOUNT")]
public int SecondPollCount { get; set; }
[Column("WAL_ATTENDEDWEBINAR")]
public int AttendedWebinar { get; set; }
[Column("WAL_MAKEUP")]
public int Makeup { get; set; }
[Column("WAL_COMMENTS")]
public string Comments { get; set; }
[Column("WAL_REGISTRANTKEY")]
public string RegistrantKey { get; set; }
[Column("WAL_WEBINARKEY")]
public string webinarKey { get; set; }
}
When the form is submitted, I am passing the model to a function to store the records in EF6.
public ActionResult PostAttendees(ICollection<WebinarAttendeesList> attendees)
{
foreach (WebinarAttendeesList attendee in attendees)
{
UpdateAttendee(attendee);
}
}
How would I edit the model to allow this delete the records that are selected and update the ones that don't have the checkbox selected?
If I put an int delete property on the model that has no Column attribute I get this exception:
ORA-00904: "Extent1"."delete": invalid identifier
I found this tutorial but I'm NOT using any helpers in the creation of the form and do not have any ViewModels and it also doesn't explain how to handle doing different things to the different records based on the checkbox: http://johnatten.com/2014/01/05/asp-net-mvc-display-an-html-table-with-checkboxes-to-select-row-items/
Is there a better way to do this?
Yes. All models properties in EF are suppose to be column. You should use NotMapped attribute if you don't want property to be treated as a 'column' in database.