Entity Framework virtual ICollections - asp.net-core

I'm new to .NET Core Entity Framework (code-first), but do daily progress.
I am now stuck on a probably small mistake and can't go on.
I have a class list that I am trying to fill and in there there is a virtual ICollection from another list, to be filled at the same time.
These are my classes
public class UgInfo
{
public Guid UserGroupId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<UInfo> UInfo { get; set; }
}
public class UInfo
{
public string UserEmail { get; set; }
public string UserName { get; set; }
}
This is where the error occurs:
Invalid Initializer member declarator"
Code:
var ugList = (from ug in _context.Usergroups
join uug in _context.UserUsergroup on ug.UserGroupId equals uug.UsergroupId
join u in _context.Users on uug.UserId equals u.UserId
select new UgInfo
{
UserGroupId = uug.UsergroupId,
Description = ug.Description,
Name = ug.Name,
new UInfo //Error
{
UserName = u.UserName,
UserEmail = u.Email
}
}).ToList();
return ugList;
Could there be anyone who can help a beginner?

Well, you're missing the member name you're attempting to initialize, for one. Then, you need to initialize it with a collection type, not a single UInfo instance:
...
Name = ug.Name,
UInfo = new List<UInfo>
{
new UInfo
{
...
}
}

You have ICollection<UInfo> as property, and you are using new UInfo in the code. It should be new List<UInfo>.

Related

How to bind custom model class in mvc

I am new in MVC. I am working on a project where i have created a model class and also context class which is working good if i view the record in normal view.
but if i try to get the data in group by "Series_Name" and bind it into same model class it gives error. here is my code
Here is Model class and DBContextClass
[Table("tblvideo")]
public class TVSerial
{
[Key]
public Int64 Video_ID { get; set; }
public string Series_Name { get; set; }
public string Season_No { get; set; }
public string Episode_No { get; set; }
public string Episode_Name { get; set; }
public string Time_Duration { get; set; }
public string File_Url_480p { get; set; }
public string File_Url_720p { get; set; }
public string Description { get; set; }
public bool Is_Active { get; set; }
public string Image_Url_Small { get; set; }
public string Image_Url_Big { get; set; }
}
public class TvSerialContext : DbContext
{
public DbSet<TVSerial> TvSerials { get; set; }
}
Here is controller class:
public class TvSerialController : Controller
{
public ActionResult ListAllTvSerial()
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial> tv = tvContext.TvSerials.ToList();
return View(tv);
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
}
Above code works as expected, but if i am doing this :
public ActionResult ListAllSeason(string serial)
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial> tv = tvContext.TvSerials.Where(tvs => tvs.Series_Name == serial).Distinct().ToList();
return View(tv);
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
it return all rows , i just want single row from every series_name and custom field "Series_Name,Season_No,Image_Url_Big"
i don't know how to achieve this.
getting result :
Expected result:-
You could do this by creating a view model and using a .GroupBy() clause
public class TVSerialVM
{
public string SeriesName { get; set; }
public string SeasonNo { get; set; }
public string ImageUrl { get; set; }
}
and the query to project into your view model
List<TVSerialVM> model = tvContext.TvSerials.Where(t => t.Series_Name == serial)
.GroupBy(t => new { t.Series_Name, t.Season_No, t.Image_Url_Big })
.Select(t => new TVSerialVM
{
SeriesName = t.Key.Series_Name,
SeasonNo = t.Key.Season_No,
ImageUrl = t.Key.Image_Url_Big
}).ToList();
Side note: Your duplicating data in the database (the season number and the image url). You should consider moving the image urls to another table with a relationship to the season number.
The reason you are getting multiple values even though you are using distinct is the Distinct method does not know what "equal" is for TVSerial.
You can use Distinct with IEqualityComparer.
https://msdn.microsoft.com/en-us/library/vstudio/bb338049(v=vs.100).aspx
Distinct is not guaranteed to on custom objects it doesn't know what to compare. I have used this SO in the past to make my custom object work with Distinct.
Creating a distinct list of custom type in C#

Convert Model into a ViewModel [duplicate]

This question already has answers here:
Where to convert business model to view model?
(3 answers)
Closed 5 years ago.
I have a model for my table OWNER
namespace MyApp.Models
{
public partial class Owner
{
public int owner_id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
public string city { get; set; }
public virtual User User { get; set; }
}
}
and then i have my ViewModel
public partial class OwnerData
{
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
}
My Controller
public ActionResult Index()
{
//Create the object Owner based on the User Identity
Owner owner = CustomDbFunctions.GetUserEntity(User, db).Owner;
//New instance of the ViewModel
OwnerData ownerData = new OwnerData();
ownerData.lastname = owner.lastname;
ownerData.firstname = owner.firstname;
ownerData.address = owner.address;
return View(ownerData);
}
There's a simpler way in order to convert my owner into ownerData without rewrite evry single property of my object?
I think i solved the problem using Automapper
public ActionResult Index()
{
//Create the object Owner based on the User Identity
Owner owner = CustomDbFunctions.GetUserEntity(User, db).Owner;
Mapper.CreateMap<Owner, OwnerData>();
OwnerData ownerData = Mapper.Map<Owner, OwnerData>(owner);
return View(ownerData);
}
Or if you prefer a dynamic function
static TDestination GenerateMappingTable<TSource, TDestination>(TSource inputModel)
{
Mapper.CreateMap<TSource, TDestination>();
return Mapper.Map<TSource, TDestination>(inputModel);
}
To install Automapper just execute
PM> Install-Package AutoMapper
from the package manager console.
You can use object initizalizers.That makes it more simple,but I'm not sure this is what you want
OwnerData ownerData = new OwnerData {
lastname = owner.lastname;
firstname = owner.firstname;
address = owner.address;
};

Dapper - Object reference not set to an instance of an object

I've recently been playing around with dapper, but I've run into a bit of a problem regarding getting data from other tables.
My database has two tables, Users and Post. I've created two classes for these tables.
My User class
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime JoinDate { get; set; }
}
My Post class
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public DateTime PublishDate { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
This is my query to fetch all the post data from my database, and also get the Username who created the post.
public IEnumerable<Post> GetPosts()
{
string myQuery = "SELECT p.Title, p.PublishDate, p.PostId, p.UserId, u.Username FROM Post p INNER JOIN Users u ON p.UserId = u.UserId";
sqlConnection.Open();
var posts = sqlConnection.Query<Post>(myQuery);
return posts;
}
Here is my view code where I want to display the information:
#model IEnumerable<MvcDapperIntro.Models.Post>
#{
ViewBag.Title = "Posts";
}
#foreach (var post in Model)
{
<h3>#post.Title</h3>
<p>#post.PublishDate.ToString("dd/MM/yyyy")</p>
<p>#post.User.Username</p>
}
However, when I run my code, I get the 'Object reference not set to an instance of an object' error. Is this because I shouldn't be using:
public User User { get; set; }
to access the username property, or should I create a new class with a username field. Any input would be appreciated.
I get the 'Object reference not set to an instance of an object' error
I bet the issue is on this line: <p>#post.User.Username</p> and that is because you did not map the User object. You should change this code:
var posts = sqlConnection.Query<Post>(myQuery);
to this:
var posts = sqlConnection.Query<Post, User, Post>(
myQuery,
(p, u) => { p.User = u; return p; }
);

RavenDB TransformResults

I'm trying to use the TransformResults feature, and I can't get it to work. I'm not totally sure I understand this feature, perhaps there is another way to solve this problem. What I want is just the Id from the Order and the email addesses from the Customer and the Entrepreneur. I am happy for all tips that can take me in the right direction. Here is my code.
Document
public class OrderDocument
public string Id {get; set }
public EntrepreneurInfo EntrepreneurInfo { get; set; }
public CustomerInfo CustomerInfo { get; set; }
public OrderStatus CurrentOrderStatus { get; set; }
}
Info classes
public class EntrepreneurInfo
{
public string EntrepreneurDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class CustomerInfo
{
public string CustomerDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
The info classes are just subsets of a Customer and Entrepreneur documents respectively.
The Customer and Entrepreneur documents inherits from a base class ( AbstractOrganizationDocument) that has the EmailAddress property.
My Index
public class OrdersApprovedBroadcastingData :
AbstractIndexCreationTask<OrderDocument, OrdersApprovedBroadcastingData.ReduceResult>
{
public OrdersApprovedBroadcastingData()
{
this.Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
Id = d.Id,
CustomerId = d.CustomerInfo.CustomerDocumentId,
EntrepreneurId = d.EntrepreneurInfo.EntrepreneurDocumentId
};
this.TransformResults = (db, orders) => from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurId)
select
new
{
o.Id,
o.CustomerId,
CustomerEmail = customer.EmailAddress,
o.EntrepreneurId,
EntrepreneurEmail = entrepreneur.EmailAddress
};
}
public class ReduceResult
{
public string Id { get; set; }
public string CustomerId { get; set; }
public string CustomerEmail { get; set; }
public string EntrepreneurId { get; set; }
public string EntrepreneurEmail { get; set; }
}
}
If I look at the result of this Index in Raven Studio I get null values for all fields except the Id. And finally here is my query.
Query
var items =
this.documentSession.Query<OrdersApprovedBroadcastingData.ReduceResult, OrdersApprovedBroadcastingData>()
.Select(x => new OrdersToBroadcastListItem
{
Id = x.Id,
CustomerEmailAddress = x.CustomerEmail,
EntrepreneurEmailAddress = x.EntrepreneurEmail
}).ToList();
Change your index to:
public class OrdersApprovedBroadcastingData : AbstractIndexCreationTask<OrderDocument>
{
public OrdersApprovedBroadcastingData()
{
Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
};
TransformResults = (db, orders) =>
from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerInfo.CustomerDocumentId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurInfo.EntrepreneurDocumentId)
select new
{
o.Id,
CustomerEmailAddress = customer.EmailAddress,
EntrepreneurEmailAddress = entrepreneur.EmailAddress
};
}
}
Your result class can simply be the final form of the projection, you don't need the intermediate step:
public class Result
{
public string Id { get; set; }
public string CustomerEmailAddress { get; set; }
public string EntrepreneurEmailAddress { get; set; }
}
You don't have to nest this class in the index if you don't want to. It doesn't matter either way. You can query either with:
var items = session.Query<Result, OrdersApprovedBroadcastingData>();
Or with
var items = session.Query<OrderDocument, OrdersApprovedBroadcastingData>().As<Result>();
Though, with the first way, the convention tends to be to nest the result class, so really it would be
var items = session.Query<OrderDocument.Result, OrdersApprovedBroadcastingData>();
Note in the index map, I am not including any properties at all. None are required for what you asked. However, if you want to add a Where or OrderBy clause to your query, any fields you might want to filter or sort on should be put in there.
One last thing - the convention you're using of OrderDocument, CustomerDocument, EntrepreneurDocument, is a bit strange. The usual convention is just Order, Customer, Entrepreneur. Think of your documents as the persisted form of the entities themselves. The convention you are using will work, it's just not the one usually used.

Entity Framework Creates New Record in Table I Didn't Reference when Inserting Into Other Table

In this website, users can register under a username and password, and can also post comments on articles. The models are pretty straightforward:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool IsAdmin { get; set; }
public DateTime JoinDate { get; set; }
public string AvatarPath { get; set; }
public string EmailAddress { get; set; }
}
public class ArticleComment
{
public int Id { get; set; }
public int ArticleId { get; set; }
public int UserId { get; set; }
public string CommenterName { get; set; }
public string Message { get; set; }
public DateTime CommentDate { get; set; }
public User User { get; set; }
}
Entity Framework correctly made the foreign key relationship between UserId on ArticleComment and Id on User when the database was created using code-first.
Here's my code for when a user posts a new comment:
public JsonResult SubmitComment(int articleId, string comment)
{
var response = new JsonResponse();
var currentUser = _userRepository.GetUserByUsername(User.Identity.Name);
//...
var newComment = new ArticleComment
{
ArticleId = articleId,
CommentDate = DateTime.Now,
CommenterName = currentUser.Username,
UserId = currentUser.Id,
User = currentUser,
Message = comment,
};
try
{
_articleRepository.Insert(newComment);
}
catch (Exception e)
{
response.Success = false;
response.AddError("newComment", "Sorry, we could not add your comment. Server error: " + e.Message);
return Json(response);
}
response.Success = true;
response.Value = newComment;
return Json(response);
}
The values that make up the newComment object all appear to be correct, and the Insert method in my Article repository class is straight and to the point:
public void Insert(ArticleComment input)
{
DataContext.ArticleComments.Add(input);
DataContext.SaveChanges();
}
But once this happens, poof: a new record in my Users table appears along with the new record in ArticleComments. All of the info in the new Users record is duplicated from that user's existing record - the only difference is the value for the primary key Id. What gives?
In addition to my comment, you need to make sure that both _userRepository and _articleRepository are using the same DbContext instance.
Either that, or you can try this:
var newComment = new ArticleComment
{
ArticleId = articleId,
CommentDate = DateTime.Now,
CommenterName = currentUser.Username,
UserId = currentUser.Id,
// User = currentUser, let the UserId figure out the User, don't set it yourself.
Message = comment,
};