I'm at wits end here. The problem is I'm trying to create a new entity in the database, with a db generated identity property, in LINQ to SQL and then create another entity associated with the first one. I'm guessing the problem is that LINQ to SQL cannot insert the second entity without the id from the first entity, which won't be known until the database generates it.
Has anyone else had this problem... if so how did you resolve it? I know I could call SubmitChanges between the creation of the first and the second entity, but this would break the transactional integrity of the program.
Here's a concrete example:
[Table(Name = "Searches")]
public class Search
{
// Db Generated Key
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public Int32 SearchID { get; set; }
// Each search can have multiple search parameters
private EntitySet<SearchParam> searchParams;
[Association(Storage = "searchParams", ThisKey = "SearchID", OtherKey = "SearchID")]
public EntitySet<SearchParam> SearchParams
{
get
{
return searchParams;
}
set
{
searchParams.Assign(value);
}
}
}
[Table(Name = "SearchParams")]
public class SearchParam
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public Int32 SearchParamID { get; set; }
[Column]
public String ParamValue { get; set; }
// Each search param is associated with one search
[Column]
public Int32 SearchID { get; set; }
private EntityRef<Search> search = new EntityRef<Search>();
[Association(Storage = "search", ThisKey = "SearchID", OtherKey = "SearchID", IsForeignKey = true)]
public Search Search
{
get
{
return search.Entity;
}
set
{
search.Entity = value;
}
}
}
So with the above code, if I were to do something like the following, .NET gives me a NullReferenceException on SubmitChanges:
using (SampleDataContext context = new SampleDataContext())
{
Search search = new Search();
search.SearchParams.Add(new SearchParam() { ParamValue = "...paramvalue..." });
context.Searches.InsertOnSubmit(search);
context.SubmitChanges();
}
It doesn't look to me like you are initializing the SearchParams list before calling add on it. Does this work:
using (SampleDataContext context = new SampleDataContext()) {
Search search = new Search();
search.SearchParams = new EntitySet<SearchParam>(); //<-- THIS LINE
search.SearchParams.Add(new SearchParam() { ParamValue = "...paramvalue..." });
context.Searches.InsertOnSubmit(search);
context.SubmitChanges();
}
Don't know why yours is not working, but this should work
using (SampleDataContext context = new SampleDataContext())
{
Search search = new Search();
SearchParam param = new SearchParam() { ParamValue = "...paramvalue..." })
param.search = search
context.searchparams.InsertOnSubmit (param ) // Not sure if this is necessary
context.Searches.InsertOnSubmit(search);
context.SubmitChanges();
}
And all in one transaction.
Try adding the following to your Search class
public Search()
{
this.SearchParams = new EntitySet<SearchParams >(
new Action<SearchParams >(this.attach_SearchParams ),
new Action<SearchParams >(this.detach_SearchParams ));
}
private void attach_SearchParams(SearchParams sp)
{
sp.Search = this;
}
private void detach_Search(SearchParams sp)
{
sp.Search = null;
}
Related
I'm trying to insert into two releated tables with linq2sql but my code only inserts one entity(Email), I dont get any exceptions - just the other entity (attachment) is not inserted.
I think I have an mistake somewhere in the association, but i cant figure out how to set it up properly.
Thanks for help.
Insert code:
using (TransactionScope main_transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(120)))
{
foreach (var attachment in attachmets)
email.Attachments.Add(attachment);
_emails.InsertOnSubmit(email);
_context.SubmitChanges();
main_transaction.Complete();
}
Pocos:
[Table(Name = "maily")]
internal class Email
{
private EntitySet<Attachment> _attachments;
public Email()
{
this._attachments = new EntitySet<Attachment>();
}
[Column(IsPrimaryKey = true, IsDbGenerated = true, Name = "ID_mailu", AutoSync = AutoSync.OnInsert)]
public virtual int ID_mailu { get; set; }
[Association(Storage = "_attachments", OtherKey = "id_mailu")]
public ICollection<Attachment> Attachments
{
get { return _attachments.ToList(); }
set { _attachments.Assign(value); }
}
}
[Table(Name = "MailPrilohy")]
internal class Attachment
{
private EntityRef<Email> _email;
public Attachment()
{
_email = default(EntityRef<Email>);
}
[Column(IsPrimaryKey =true, IsDbGenerated = true, Name = "id_prilohy", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id_Prilohy { get; set; }
[Column(Name = "id_mailu", CanBeNull = false, IsDbGenerated = true)]
public int id_mailu { get; set; }
[Association(Storage = "_email", ThisKey = "id_mailu", OtherKey = "ID_mailu", IsForeignKey = true)]
public Email Email
{
get { return _email.Entity; }
set { _email.Entity = value; }
}
}
}
So I have just solved the problem...
The problem was in several places.
1s was the association missed foreign key attribute ..
[Association(Storage = "_email", ThisKey = "id_mailu", OtherKey = "ID_mailu", IsForeignKey = true)]
public Email Email
{
get { return _email.Entity; }
set { _email.Entity = value;}
}
Next, i was supposed to insert attachments -> not the email.
_context.Attachments.InsertAllOnSubmit(attachmets);
Can you try to see if this works for you?
using (TransactionScope main_transaction = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(120)))
{
foreach (var attachment in attachmets)
attachment.Email = email; // only this line changed
_emails.InsertOnSubmit(email);
_context.SubmitChanges();
main_transaction.Complete();
}
I have a database that has two tables as follows, Please ignore the data but the format looks as follows
Now I have a Model class that is constructed as follows
public class FamilyModel
{
public string Name { get; set; }
public List<FamilyModel> FamilyList { get; set; }
public FamilyModel()
{
FamilyList = new List<FamilyModel>();
}
}
Now all I want is to get data from the two tables and populate the list.
So I have a stored procedure that returns data as follows
So I have written some code to populate the above class. But it dosent work. I get a count of 5 when I debug. I want the count to be 2 and when expanded I want something like FamilyA ->{Nick, Tom, Pam}.. FamilyB->{Harry} and so on. Please help fixing this code.
public static FamilyModel familyData()
{
//FamilyModel fml = new FamilyModel();
//fml.FamilyList = new List<FamilyModel>();
using (SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\Families.mdf; Integrated Security=True; Connect Timeout=30;"))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_GetFamilies", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
FamilyModel fm = new FamilyModel();
fm.Name = dr["FamilyName"].ToString();
foreach (var item in dr["ChildName"].ToString())
{
if (Convert.ToInt32(dr["id"]) == Convert.ToInt32(dr["FID"]))
{
fm.FamilyList.Add(new FamilyModel() { Name = dr["ChildName"].ToString() });
}
}
}
return fm;
}
}
Here is some source code that should get the right idea across. Below it, I've included some explanation for what's going on.
using Dapper;
public class FamilyModel
{
public int Id { get; set;}
public string FamilyName { get; set; }
public List<Person> Members { get; set; } = new List<Person>();//Initializer for Auto-Property, C#6<= only
}
public class Person
{
public int Id { get; set;}
public string Name { get; set; }
}
public class DatabasePOCO
{
public string FamilyName { get; set; }
public string ChildName { get; set; }
public int Fid { get; set; }
public int Id { get; set;}
}
void Main()
{
using (IDbConnection conn = new SqlConnection("..."))
{
conn.Open();
var raw = conn.Query<DatabasePOCO>("sp_GetFamilies",
commandType: CommandType.StoredProcedure);//Could be dynamic, not typed
var familyList = raw
.GroupBy(x => x.Fid)
.Select(x =>
{
var rawMembers = x.ToList();
var fId = x.First().Fid;
var fName = x.First().FamilyName;
var members = rawMembers.Select(y => new Person
{
Id = y.Id,
Name = y.ChildName
});
return new FamilyModel
{
Id = fId,
FamilyName = fName,
Members = members.ToList()
};
});
//Whatever else you want to do here
}
}
Consider using Dappper. It is a great ORM that makes accessing data from database really easy. It's designed to work with SQL Server, but I've had success using Oracle too, and most other RMDBS systems will probably work.
Consider using Slapper. If you have control over your stored procedure, this can reduce a lot of the boilerplate code below.
If you use Dapper (I hope you do), you can play around with C# dynamic objects, or you can create a POCO to help get some type enforcement on your code.
Understand if you care about reference equality. The code I provided below does not enforce reference equality of objects. Reference equality, in my experience, doesn't buy you much and is a pain to enforce.
You need to distinguish between a new row in the data set and a new FamilyModel. One way to do this is to declare a list of models, then look up the "right" one before you add the current child row:
var rootModel = new FamilyModel();
rootModel.Name = "root";
// ... Set up data reader ...
while (dr.Read())
{
//This requires support for the Id in your FamilyModel:
var id = (int)dr["Id"];
//You could also use ".Single(...)" here
var fm = rootModel.FamilyList.Where(x => x.Id == id).First();
if (fm == null)
{
fm = new FamilyModel();
fm.Name = dr["FamilyName"].ToString();
rootModel.FamilyList.Add(fm);
}
fm.FamilyList.Add(new FamilyModel() { Name = dr["ChildName"].ToString() });
}
For each row in your database query, you'll:
Try to look up that family in your list
If you don't find one, create a new one. Add it to your top-level list.
Add the child name as a sub-element of the "current" family.
I have a method that is calling a stored procedure and returning a list of objects. I am not sure how to add the result from the stored procedure to the list of objects. I tried to use model. Add but anyway I am using it I am getting error. I have identify the place I need help inside the code.
This is my code:
public List<Models.Type> get_Type(string Type_Group)
{
string connectionName = getConnectionStr();
List<Models.Type> model = null;
string myConnection = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ToString();
SqlDatabase db = new SqlDatabase(myConnection);
using (DbCommand command = db.GetStoredProcCommand("LA_Get_Type"))
{
db.AddInParameter(command, "Type_Group", DbType.String, Type_Group);
var result = db.ExecuteReader(command);
try
{
if (result.FieldCount == 0)
model = null;
else
{
while (result.Read())
{
model = new List<Models.Type>()
{
//This is the place I don't know I tried model.Add but not sure what
to have after.
This code is when I have returning just 1 object but I want to
return list of objects
typeID = Convert.ToInt32(result["typeID"].ToString()),
type_group = result["type_group"].ToString(),
type_value = result["type_value"].ToString(),
type_desc = result["type_desc"].ToString(),
type_sort = Convert.ToInt32(result["type_sort"].ToString())
};
}
}
}
catch (Exception ex)
{
}
result.Close();
return model;
}
}
and this is my object:
public class Type
{
public int typeID { get; set; }
public string type_group { get; set; }
public string type_value { get; set; }
public string type_desc { get; set; }
public int type_sort { get; set; }
}
Change
while (result.Read())
{
model = new List<Models.Type>()
{
//This is the place I don't know I tried model.Add but not sure what
to have after.
This code is when I have returning just 1 object but I want to
return list of objects
typeID = Convert.ToInt32(result["typeID"].ToString()),
type_group = result["type_group"].ToString(),
type_value = result["type_value"].ToString(),
type_desc = result["type_desc"].ToString(),
type_sort = Convert.ToInt32(result["type_sort"].ToString())
};
}
to something like this:
model = new List<Models.Type>();
while (result.Read())
{
Models.Type aModel = new Model(){
typeID = Convert.ToInt32(result["typeID"].ToString()),
type_group = result["type_group"].ToString(),
type_value = result["type_value"].ToString(),
type_desc = result["type_desc"].ToString(),
type_sort = Convert.ToInt32(result["type_sort"].ToString())
};
model.Add(aModel);
}
Note that I'm creating a new object for each result and then adding them one by one to the list.
I'm trying to set up a one to many relationship with linq on Windows Phone 8.
My problem is that in the EntitySet<> field only one of the classes which should be stored, really gets stored to the database.
So i made a simple Project to give you clue of my problem.
There are two classes Person and Number. One Person can have many numbers.
Here is the number class:
[Table]
class Number
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int NumberID { get; set; }
[Column]
public int PhoneNumber { get; set; }
}
And the Person class:
[Table]
class Person
{
private EntitySet<Number> numbers = new EntitySet<Number>();
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int PersonID { get; set; }
[Column]
public string Name { get; set; }
[Association(Storage = "numbers", ThisKey = "PersonID", OtherKey = "NumberID")]
public EntitySet<Number> Numbers
{
get { return numbers; }
set
{
numbers.Assign(value);
}
}
}
When i now try to insert a person with three numbers:
EntitySet<Number> numbers = new EntitySet<Number>();
DataBase db = new DataBase(App.DBConnectionString);
Number num1 = new Number() { PhoneNumber = 111111 };
Number num2 = new Number() { PhoneNumber = 222222 };
Number num3 = new Number() { PhoneNumber = 333333 };
Person person = new Person() { Name = "Donald" };
numbers.Add(num1);
numbers.Add(num2);
numbers.Add(num3);
person.Numbers = numbers;
try
{
db.Persons.InsertOnSubmit(person);
db.SubmitChanges();
}
catch (Exception s)
{
// do nothing
}
And then try to retrieve the data again:
DataBase db = new DataBase(App.DBConnectionString);
string text = "";
foreach (Person person in db.Persons)
{
foreach (Number num in person.Numbers)
{
text += num.PhoneNumber + System.Environment.NewLine;
}
}
TextBlock.Text = text;
i only get the first phone number which is '111111'. Interesting is, that on the second run the text string holds '111111' and '222222'. This is because in the second Person the second phone number is saved and in the third person the third phone number and so on.
So if you do that often enough you get:
111111
222222
333333
111111
222222
333333
...
Btw if you jump with the debugger to 'db.Persons.InsertOnSubmit(person);' and look into 'person' there is a list with all three numbers in it. So it should work...
I tried really hard, but i can't figure out a way to get it right. My suspicion though is on the [Association] attribute.
Here is a link to the source so you can get the whole picture if you want: https://docs.google.com/file/d/0B5G4zya_BlZyUlU0azVvbVdiajA/edit?usp=sharing
I also had that problem a while ago. I added a few lines to my code that made it disappear.
Linq2SQL is a bit magical and sometimes things (don't) work for no obvious reason. Just to be perfectly sure you might want to implement the official One-To-Many solution provided by Microsoft:
http://msdn.microsoft.com/en-us/library/vstudio/bb386950(v=vs.100).aspx
Also I would make the following adjustments:
[Table]
class Number
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int NumberID { get; set; }
[Column]
public int _personID { get; set; }
[Column]
public int PhoneNumber { get; set; }
private EntityRef<Person> _person;
[Association(Storage = "_person", ThisKey = "_personID", OtherKey = "PersonID", IsForeignKey = true)]
public Person Person
{
get { return this._person.Entity; }
set {
this._person.Entity = value;
if (value != null)
{
this._personID = value.PersonID;
}
}
}
}
And I'd add this to the constructor:
public Person()
{
this._numbers = new EntitySet<Number>(
delegate (Number entity)
{
entity.Person = this;
},
delegate (Number entity)
{
entity.Person = null;
});
}
As a test I would not try to load the entire object. First just check if all numbers have made it to the database:
var numbers = db.Numbers.ToList();
This is because L2S on Windows Phone has serious trouble reading deep object connections. One level is fine but deeper relationships are ignored. So if your Person class resides in another class that could be a problem as well.
Edit: You can add options to your DB context in order to force deep object loading:
_db = new DBContext("isostore:/mydb.sdf");
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Person>(p => p.Numbers);
_db.LoadOptions = loadOptions;
I have spent hours trying to figure out why my database cannot find the table I have found numerous examples and none have seemed to help. I have created a separate class to handle the database operations so I can use it on multiple pages.
Here is the code
[Table]
public class MatchItem
{
[Column(IsPrimaryKey = true, CanBeNull=false,IsDbGenerated=true)]
public int MatchID { get; set; }
[Column(CanBeNull = false)]
public string MatchNumber { get; set; }
[Column(CanBeNull = false)]
public string EventName { get; set; }
[Column(CanBeNull = false)]
public DateTime Time { get; set; }
[Column(CanBeNull = false)]
public string[] RedTeams { get; set; }
[Column(CanBeNull = false)]
public string[] BlueTeams { get; set; }
[Column(CanBeNull = false)]
public int RedFinal { get; set; }
[Column(CanBeNull = false)]
public int BlueFinal{ get; set; }
}
Here is the Data context
public class MatchDataContext:DataContext
{
public MatchDataContext(string connectionString) :
base(connectionString)
{
}
public Table<MatchItem> Matches
{
get
{
return this.GetTable<MatchItem>();
}
}
}
I made a class so I could use it on multiple pages
public class MatchDBManager
{
private static string connectionString = #"Data Source=isostore:/DB.sdf";
public MatchDBManager()
{
initialize();
}
public void initialize()
{
using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
{
if (Mchdb.DatabaseExists())
{
Console.WriteLine("DB already exists");
}
else
{
Mchdb.CreateDatabase();
Console.WriteLine("DB created");
}
}
}
public void addMatchData(IList<MatchItem> data)
{
//this.clearData();
//initialize();
using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
{
Mchdb.Matches.InsertAllOnSubmit(data);
Mchdb.SubmitChanges();
}
}
public IList<MatchItem> getTeamData(string teamNum)
{
IList<MatchItem> MatchList = null;
using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
{
IQueryable<MatchItem> mchQuery = from mch in Mchdb.Matches where (mch.RedTeams[0] == teamNum || mch.RedTeams[1] == teamNum || mch.RedTeams[2] == teamNum || mch.BlueTeams[0] == teamNum || mch.BlueTeams[1] == teamNum || mch.BlueTeams[2] == teamNum) select mch;
MatchList = mchQuery.ToList();
}
return MatchList;
}
public IList<MatchItem> getEventData(string eventID)
{
IList<MatchItem> MatchList = null;
using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
{
IQueryable<MatchItem> mchQuery = from mch in Mchdb.Matches where mch.Event == eventID select mch;
MatchList = mchQuery.ToList();
}
return MatchList;
}
private void clearData()
{
using (MatchDataContext Mchdb = new MatchDataContext(connectionString))
{
if (Mchdb.DatabaseExists())
{
Mchdb.DeleteDatabase();
}
}
}
}
I have the error The specified table does not exist[Match].
Added here is where I download
public IList<MatchItem> ParseXML(XmlReader reader)
{
//List<string> save = new List<string>();
List<MatchItem> MatchList= new List<MatchItem>();
XElement matchData;
matchData = XElement.Load(reader);
StringBuilder output = new StringBuilder();
int count = 0;
var matches = from item
in matchData.Elements("match")
select item;
foreach (XElement eachmatch in matches)
{
MatchItem mch = new MatchItem();
string Time = ((eachmatch.Element("pubdate").Value).ToString());
mch.EventName = ((eachmatch.Element("event").Value).ToString());
mch.MatchNumber = ((eachmatch.Element("mch").Value).ToString() + (eachmatch.Element("typ").Value).ToString());
string[] RT = { eachmatch.Element("red1").Value.ToString(), eachmatch.Element("red2").Value.ToString(), eachmatch.Element("red3").Value.ToString() };
string[] BT = { eachmatch.Element("blue1").Value.ToString(), eachmatch.Element("blue2").Value.ToString(), eachmatch.Element("blue3").Value.ToString() };
string RF = ((eachmatch.Element("rfin").Value).ToString());
string BF = ((eachmatch.Element("bfin").Value).ToString());
// Time = Time.Substring(0, (Time.IndexOf("+") - 1));
mch.Time = DateTime.Parse(Time);
mch.RedTeams = RT;
mch.BlueTeams = BT;
mch.RedFinal = int.Parse(RF);
mch.BlueFinal= int.Parse(BF);
mch.MatchID = count;
count += 1;
MatchList.Add(mch);
}
return MatchList;
}
This is where I call this method
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
initializeDB();
if (e.Error == null)
{
XmlReader reader = XmlReader.Create(new StringReader(e.Result));
DownloadInfo di = new DownloadInfo();
IList <MatchItem>data= di.ParseXML(reader);
outputer(data);
addData(data.ToList<MatchItem>());
}
else
{
IList<MatchItem> data = getTeamData(strMyTeam);
outputer(data);
}
}
I ended up removing the DatabaseManager class and putting the functions in the main code
Then I output them to the screen here
public void outputer(IList<MatchItem> mch)
{
for (int i = 0; i < mch.Count; i++)
{
Score sc = new Score();
sc.Width = lsbData.Width;
sc.Height = sc.Height;
sc.Time = mch[i].Time.ToString();
sc.Event = mch[i].EventName;
sc.RT = mch[i].RedTeams[0] + " " + mch[i].RedTeams[1] + " " + mch[i].RedTeams[2];
sc.BT = mch[i].BlueTeams[0] + " " + mch[i].BlueTeams[1] + " " + mch[i].BlueTeams[2];
sc.RF = mch[i].RedFinal.ToString();
sc.BF = mch[i].BlueFinal.ToString();
lsbData.Items.Add(sc);
}
}
*note:score is a custom control that works(and worked) before the database code *
I don't see where you actually create a Match Object.
if you have you need to include that code in the question as well. and if you haven't, that would explain why it doesn't exist.
Addition
in order to add Match Objects to a list you will have to create the objects first then add them to the list, I don't think you can create the whole list of objects before creating each individual object.
more Additional Information
the object still needs to be created before you can add items to it. that is what the error is telling you. you don't have the object to insert data into.
Match Table1 = new Match();
this creates a new Match object which allows you to access the pieces of the object and insert data into the object like this
Table1.MatchNumber = 42
you can't add to something to a memory location until you set aside that memory location for that specific person and give it a name.
when you create that class you can add functions and all sorts of fun stuff to it, but you can't use any of it until you have created a Match Object.
you can't add something to a list that doesn't exist, you have to create the Match Object first, then add it to the list