EF Core, navigation property HasMany / WithMany without join table - c#

I have two tables:
Table Cards with the following Columns:
Id (Unique Index/Primary Key)
OracleId (Guid, NOT Unique)
CardName
CardText
SetName
...
Table CardRulings:
OracleId (Guid, Indexed, NOT Unique)
Source
Comment
I'm looking for a simple navigation property. Card.Rulings should have a collection (ICollection<CardRuling>) of associated CardRulings from the table. CardRulings is a keyless table and there are duplicate OracleId's in both the Cards and CardRulings table. This isn't quite a HasMany/WithMany as there is no key technically in either table, and there is no "unique" index that links the two tables together.

Try this:
var cards = _dbContext.Cards.select(c=>new CardsDTO(){
Id = c.Id,
OracleId= c.OracleId,
CardName = c.CardName,
CardText = c.CardText,
SetName = c. SetName,
CardRulings = new List<CardRulings>();
});
var cardRuling = _dbContext.CardRulings.ToList();
foreach(var item in cards){
item.CardRulings = cardRuling.Where(f=> f.OracleId = item.OracleId).ToList()
}

Related

How to select and update specific columns by Entity Framework Core?

I'm trying to convert raw SQL to EF core now.
my table has many columns, such as column1 to 10, and I need to select and update specific columns. original code like this :
SELECT column1, column2 FROM table WHERE key = "someKey"
(processing data)
UPDATE table SET column2 = someValue WHERE key = "someKey"
first, I tried like this :
var query =
from model in context
where key == "someKey"
select model;
query.First().column2 = someValue;
context.SaveChanges();
this code works very fine as I wished, but SQL generated like this :
SELECT key, column1, column2, ... column10 FROM table WHERE key = "someKey"
I do not want select useless columns so I tried this:
var query =
from model in context
where key == "someKey"
select new myDTO
{
Item1 = model.column1,
Item2 = model.column2
};
query.First().Item2 = someValue;
context.SaveChanges();
this code generates SELECT SQL statement exactly I wished, but cannot generate update statement. (obviously, myDTO is not registered into DbContext)
How can I do this with EF Core?
You can use Attach and Entry methods to track the changes to a entity model. To identify the model you would need all the keys (here I'm considering only one primary key: Id)
var query =
from model in context
where key == "someKey"
select new myDTO
{
Id = model.Id,
Item1 = model.column1,
Item2 = model.column2
};
var dto = query.First();
// Here I'm using Entity but you should use the right type
var entityyModified = new Entity();
entityModified.Id = dto.Id;
entityyModified.Item1 = dto.Item1;
entityyModified.Item2 = dto.Item2;
// ...
// Item1 or Item2 properties can be assigned to different values
// ...
// Save the changes
context.Attach(entityyModified);
var dbEntry = context.Entry(entityyModified);
dbEntry.Property(e => e.Item1).IsModified = true;
dbEntry.Property(e => e.Item2).IsModified = true;
context.SaveChanges();

Join two tables in nhibernate mvc c#

I am joining two tables using primary key and foreign key.
My tables are
Item, Columns are:
ItemId int primary key,
Name varchar,
Price float,
CategoryID int foreign key,
QtyInStock int
Category, Columns are:
Id int pk,
Category varchar,
Name varchar
I want to select Category on basis of ID=categoryId
I am using NHibernate 4.03 with xml configuration files.
For unrelated entities:
var query = from item in session.Query<Item>()
join category in session.Query<Category>() on item.CategoryID equals category.Id;
For related entities:
Category catAlias = null;
var query = session.QueryOver<Item>()
.JoinAlias(x => x.Category, () => catAlias);

Getting collection from foreign key

In such a two tables
table Person
{
int Id -> primary key
varchar name
varchar nick
int GroupId -> foreign key
}
table Group
{
int Id -> primary key
varchar name
}
If I use
var result = (from c in myDataBase.Group
select c).ToList<Group>();
I get only list of Group, but field System.Data.Objects.DataClasses.EntityCollection<Person> is empty. How should I change query to get also list of Persons?
It's not clear where System.Data.Objects.DataClasses.EntityCollection<Person> has to be, but I presume you are searching for Inner join
var query = from person in people
join group in groups on person.GroupId equals group .Id
select new {.. something... };
Here I presumed you have somewhere people (collection of Person types) and want to find all persons from that collection and their matching information from related Group.
If this is not what you're asking for, please clarify.
I solved problem with:
myDataBase.Group.Include("Person").Select(a => a).ToList();
Btw: what is equivalent of Include() in linq query from...where...select ?

Joining tables in Entity Framework

I have a table called Students and a table called Majors, Students and Majors are joined by MajorId I have set this relationship already and have set the foreign key in the schema. When I access my Student object how can I return the MajorName column (this comes from the Majors table)? The only options I have in intellisense is Major_1, Major_1Reference, MajorId .
Major_1 should be a navigation property leading to the appropriate Major entry, so you should be able to access the Major's properties like this:
from s in ctx.Students
select s.Major_1.MajorName
You can use linq join statement like this to make query on the two tables...
var q = from s in Students
join m in Majors on s.MajorId equals m.MajorId
select new { m.MajorName };

Fluent NHibernate join not using primary key

I am trying to get a single property from a joined table where a non-PK in my main table is joined to the PK of the foreign table. Below is an oversimplified example of what I am trying to accomplish (I do not want to reference the foreign entity):
Tables:
CREATE TABLE Status
(
Id int,
Body text,
CategoryId int
)
CREATE TABLE Category
(
Id int,
Name text
)
SQL to generate:
SELECT Id, Body, CategoryId, Category.Name AS CategoryName
FROM Status
LEFT JOIN Category ON Category.Id = Status.CategoryId
I am trying to map the join like this in the StatusMap but it seems to be joining on the two primary keys (where Status.Id = Category.Id):
Join("Category" m =>
{
m.Optional();
m.KeyColumn("CategoryId");
m.Map(x => x.CategoryName, "Name");
});
As far as I know the only way around this using Fluent is to map to a view as you currently are doing. Join() will always map to the primary key of the parent table. The KeyColumn method specifies the key column for the child table only, which in your case is the Category table.
To achieve the desired SQL using your simplified version above you'd probably want to use References to define a many-to-one relationship between status and category.

Categories

Resources