returning an array within a row in a stored procedure - sql-server-2012

The following is a simplified version of my set up.
If i were to have a system that keeps track of the toys that children play with, my database would have the following tables:
CREATE TABLE Children
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR NOT NULL
CREATE TABLE Toys
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR NOT NULL
CREATE TABLE Plays_With
Child_Id INT PRIMARY KEY,
Toy_Id INT PRIMARY KEY,
CONSTRAINT FK_Child FOREIGN KEY Child_Id REFERENCES Children(Id),
CONSTRAINT FK_Toy FOREIGN KEY Toy_Id REFERENCES Toys(Id)
My C# classes would look like this:
public class Child
{
public int Id {get; set;}
public string Name {get; set;}
public List<Toy> Toys {get; set;}
}
public class Toy
{
public int Id {get; set;}
public string Name {get; set;}
}
(Please ignore any syntax errors above as it's a description of the set up, not the actual code)
Now to the actual question, is there a way to retrieve the information necessary to fill a list of Child objects (retreiving every child and their associated toys) with a single stored procedure?

Related

Error when creating Database using Entity Framework Core

I am getting the error bellow, when I try to create my database using Entity Framework Core
Introducing FOREIGN KEY constraint 'FK_StudentAnnouncements_Students_StudentId' on table 'StudentAnnouncements' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
I have a one to many relationship between Student and StudentAnnouncements. My Student entity has the code below
public class Student
{
public int Id { get; set; }
public virtual ICollection<StudentAnnouncement> StudentAnnouncements { get; set;}
}
My StudentAnnouncement entity has the code below
public class StudentAnnouncement
{
public int Id { get; set; }
public int StudentId { get; set; }
public virtual Student Student { get; set; }
}
When I create a migration and update the database using dotnet ef database update I get this error.
Cascade delete means that when a Student is deleted, all rows referencing that Student in the StudentAnnouncements table will be deleted as well, so that there is no Student Announcement that point to an inexistent Student. What you have here is a scenario where cascade delete will have impact in multiple entities. In other words, if a Student is deleted, some other table besides StudentAnnouncements is going to have rows deleted as well and SQL Server doesn't like that ambiguity. You have to choose what you really want to be deleted and what will be kept.
It's possible to change the default behavior of cascade deleting in the DbContext OnConfiguring() method, using Fluent API. You can learn about that configuration here:
https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete#configuring-cascading-behaviors
The problem at foreign key named FK_StudentAnnouncements_Students_StudentId on table StudentAnnouncements.
Choose one of these solutions:
(1) Delete foreign key FK_StudentAnnouncements_Students_StudentId in database, re-run Entity Framework Core scaffolding.
(2) Specify ON DELETE NO ACTION
(3) Specify UPDATE NO ACTION
Sample syntax
CREATE TABLE child_table
(
col_1 data_type [ NULL | NOT NULL ],
col_2 data_type [ NULL | NOT NULL ],
…
CONSTRAINT fk_name
FOREIGN KEY (child_col_1, child_col_2, … child_col_n)
REFERENCES parent_table (parent_col_1, parent_col_2, … parent_col_n)
ON DELETE CASCADE
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
);
Let's me know result.

Two entities having one to many relationship with another entity

I have three enttites A,B and C. The relation is as follows:
A is One To many with C
B is One to many with C
If I have only one of the above, I use to keep a "id" of A/B in C as a foreign key. But in this case I am not getting how to do it.
An entry in C either belongs to A or B not both.
I am using Hibernate as ORM, and MySQL as database. Please suggest.
I Faced the same problem, and I've found the only solution is to use an abstract class, because we cannot make a foreign key that dynamic, here is the example I used:
public abstract class AbstractC{
private int id;
}
#Entity...
public class CbelongsToA extends AbstractC{
#ManyToOne...
private A a;
}
#Entity...
public class CbelongsToB extends AbstractC{
#ManyToOne...
private B b;
}
public class A{
#OneToMany
private List<CbelongsToA> cbelongToA;
}
public class B{
#OneToMany
private List<CbelongsToB> cbelongToB;
}
Sorrry I'm not so familiar with MySQL, but it may be that general decision will work in your case. In some other DBMS's I would solve this problem by creating:
Two NULL-able FK's in table C (FK's to table A and B respectively)
Table constraint checking that both FK's are not nulled or filled at
the same time.
Something like that (pseudo-sql, just for illustration):
CREATE TABLE C (
C_DATA varchar(255),
A_ID int NULL,
B_ID int NULL,
FOREIGN KEY (A_ID) REFERENCES A(ID),
FOREIGN KEY (B_ID) REFERENCES B(ID),
CONSTRAINT chk_c_fks CHECK (A_ID IS NOT NULL XOR B_ID IS NOT NULL))

Entity - one to one relationships

So I have 2 tables. Labor and Range.
Labor
LaborID
Range
RangeID
LaborID
Range has a foreign key constraint for LaborID. The constraint is unique, and I've added a unique index on the LaborID to complete the 1 to 1 relationship.
My problem is, when adding these tables to my entity object, it shows the relationship as 1 to Many. I added these tables to a diagram in SQL Server, and they clearly have a 1 to 1 relationship. What am I missing here? If I manually change the entity table's properties to 1 --> 0..1 { which it is }, then it errors out.
I don't want a 1 to many relationship. Each labor will have one and only one range.
Any thoughts or ideas?
Thanks!
public class Labor
{
[Key]
public int LaborID {get;set;}
}
public class Range
{
[Key]
[ForeignKey("Labor")]
public int RangeID {get;set;}
public virtual Labor Labor {get;set;}
}
You dont need create a new ID for "RANGE CLASS" use the same properity to be the ID and FOREIGN KEY

How to insert a record using Entity Framework with foreign key lookup in a single operation?

Let's say I want to insert a new car with its make:
public class Car {
public int Id {get;set;}
public string Name {get;set;}
public MakeId {get;set;}
}
public class Make{
public int Id {get;set;}
public string Name {get;set;}
}
Now let's say I have the Name of the make for the car I want to insert. I don't want to make a query for the make to retrieve the make id and later make a new query to insert the car. I want EF to produce something like :
INSERT INTO Cars (Name, MakeId)
SELECT #name, m.Id FROM Makes m WHERE m.Name = #makeName
Is that possible somehow or just going down to ADO.NET ?
That's not possible. You can only create the relationship from Car to Make by either setting the foreign key property Car.MakeId to a valid key value of a Make or by setting a navigation property Car.Make to a Make entity that again has a valid key value. In both cases you need to know the primary key value Id of the Make you want to assign to the Car. Knowing any other property - like Make.Name - is not sufficient, even not if it is guaranteed to be unique by a unique key constraint in the database.

Define Mapping relationship with JPA

I am having difficulties to figure out what's proper way of defining the following relationships with JPA2 mapping.
create table TableA {
id int primary key
name varchar(255) not null
);
create table TableB {
x_id int not null REFERENCES TableA(id),
y_id int not null REFERENCES TableA(id),
PRIMARY KEY (x_id, y_id)
);
Since there is a composite key involved, I know I need to define a serializable class such as FooPK.
#Embeddable
public class FooPK {
#Column(name = "x_id", insertable=true, nullable=false)
private long x_id;
#Column(name = "y_id", insertable=true, nullable=false)
private long y_id;
...
}
But I am lost on where to define #OneToMany and #ManyToOne relationship and how to properly write down the #JoinColumn() in this case.
Any help is much appreciated.
Oliver
Try to use #ManyToMany annotation.
You don't need to define the intermediate class.
More info: http://download.oracle.com/javaee/5/api/javax/persistence/ManyToMany.html