SQL Constraints Exception - Xamarin Mobile app IOS - c#

i have an issue which is driving me up the wall.
The code below adds a note into the database which is then listed out. The problem is, it adds the first note perfectly fine, then i go to add another note and i get a "Constraints" error! I just can see why.
Modal;
namespace MyApp.Models
{
[Table("notes")]
public class Note
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Unique]
public long ProgramId { get; set; }
public string source { get; set; }
public int TaskId { get; set; }
public string UserId { get; set; }
public DateTime DateAdded { get; set; }
public string Content { get; set; }
public static void addNote(Note newNote)
{
//SQLiteConnection conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB);
using (var conn = new SQLiteConnection(AssessNETApp.Data.Globals.DB))
{
try
{
conn.Insert(newNote);
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
Code for button click to add note
async void OnAddNoteButtonClick(object sender, EventArgs e)
{
if (Text.Text.Length > 0)
{
Note NoteData = new Note();
NoteData.DateAdded = DateTime.Now;
NoteData.TaskId = thisTask.TaskId;
NoteData.UserId = MyApp.Data.LoggedInUser.UserID;
NoteData.Content = Text.Text.ToString();
Note.addNote(NoteData);
await Navigation.PopAsync();
}
}
First note i add , fine! Second, error..

[Unique]
public long ProgramId { get; set; }
if you don't explicitly assign a ProgramID, it will default to 0. So the first is Unique, but the second is also 0, which violates the constraint.

I have since fixed it. For some reason i wondered if the actual data table had been altered considering they remain on the device. I have now dropped the tables so they re-create and hence this has actually removed where i did have [Unique].
Lesson learnt is that if you make changes to the Model, drop the tables else its still just running off the original unless the DB or tables are created each time you use the app.

Related

ASP.NET MVC recursion

I am working with .NET MVC that have a model implement recursion as below:
public class Comment
{
[Key]
[Required]
public int CommentId { get; set; }
[Required]
public string Content { get; set; }
public bool Anonymous { get; set; }
[Required]
public DateTime Created_date { get; set; }
[Required]
public DateTime Last_modified { get; set; }
public virtual Comment Reply { get; set; }
public virtual ICollection<Comment> Replys { get; set;}
public virtual Idea Idea { get; set; }
}
In explanation, each Idea contains different Comments inside it, and each comment also has several smaller Comments that reply to the previous one. However, I have no idea how to do recursion to get replies for a comment and smaller reply for each reply until the last one in controller and also how to display them in view. Feel free to ask me any question since my explanation is not clear sometimes.
public ActionResult Index(int? i)
{
List<Idea> Ideas = db.Ideas.ToList();
foreach(Idea idea in Ideas)
{
idea.Comments = db.Comments.Include(x => x.Reply).ToList();
idea.Comments=idea.Comments.Where(x => x.Idea == idea).ToList();
foreach (Comment comment in idea.Comments)
{
comment.Replys = GetComments(comment.CommentId); //This function use to get list of reply for comment
foreach (Comment reply in comment.Replys)
{
reply.Replys=GetComments(reply.CommentId);
foreach (Comment reply2 in reply.Replys)
{
reply2.Replys=GetComments(reply2.CommentId);
foreach(Comment reply3 in reply2.Replys)
{
reply3.Replys = GetComments(reply3.CommentId);
//When would this stop ?
}
}
}
}
}
return View(Ideas.ToPagedList(i ?? 1, 5));
}
Use this loop;
foreach(Idea idea in Ideas)
{
idea.Comments = db.Comments.Include(x => x.Reply).ToList();
idea.Comments= idea.Comments.Where(x => x.Idea == idea).ToList();
foreach (Comment comment in idea.Comments)
{
RecursionGetComments(comment);
}
}
Add this function;
public void RecursionGetComments(Comment comment){
comment.Replys = GetComments(comment.CommentId);
foreach(var reply in comment.Replys){
RecursionGetComments(reply);
}
}
If the RecursionGetComments above didn't fill properly, you might need to use the ref keyword which passes the current instance of the class (pass by reference);
public void RecursionGetComments(ref Comment comment){
comment.Replys = GetComments(comment.CommentId);
foreach(var reply in comment.Replys){
RecursionGetComments(reply);
}
}
I also noticed that in your model, you are using the keyword virtual.
Virtual signals entity framework that the navigation property will be loaded automatically / lazy loading, since you are loading your comments manually, you could remove that.

Non-frozen UDTs are not allowed inside collections CassandraCSharpDriver

I am cassandra for custom logging my .netcore project, i am using CassandraCSharpDriver.
Problem:
I have created UDT for params in log, and added list of paramUDT in Log table as frozen.
But i am getting error: Non-frozen UDTs are not allowed inside collections. I don't know why ia m getting this error because i am using Frozen attribute on list i am using in Log Model.
logSession.Execute($"CREATE TYPE IF NOT EXISTS {options.Keyspaces.Log}.{nameof(LogParamsCUDT)} (Key text, ValueString text);");
Here is model:
public class Log
{
public int LoggingLevel { get; set; }
public Guid UserId { get; set; }
public string TimeZone { get; set; }
public string Text { get; set; }
[Frozen]
public IEnumerable<LogParamsCUDT> LogParams { get; set; }
}
Question where i am doing wrong, is my UDT script not correct or need to change in model.
Thanks in advance
I've tried using that model and Table.CreateIfNotExists ran successfully.
Here is the the code:
public class Program
{
public static void Main()
{
var cluster = Cluster.Builder().AddContactPoint("127.0.0.1").Build();
var session = cluster.Connect();
session.CreateKeyspaceIfNotExists("testks");
session.ChangeKeyspace("testks");
session.Execute($"CREATE TYPE IF NOT EXISTS testks.{nameof(LogParamsCUDT)} (Key text, ValueString text);");
session.UserDefinedTypes.Define(UdtMap.For<LogParamsCUDT>($"{nameof(LogParamsCUDT)}", "testks"));
var table = new Table<Log>(session);
table.CreateIfNotExists();
table.Insert(new Log
{
LoggingLevel = 1,
UserId = Guid.NewGuid(),
TimeZone = "123",
Text = "123",
LogParams = new List<LogParamsCUDT>
{
new LogParamsCUDT
{
Key = "123",
ValueString = "321"
}
}
}).Execute();
var result = table.First(l => l.Text == "123").Execute();
Console.WriteLine(JsonConvert.SerializeObject(result));
Console.ReadLine();
table.Where(l => l.Text == "123").Delete().Execute();
}
}
public class Log
{
public int LoggingLevel { get; set; }
public Guid UserId { get; set; }
public string TimeZone { get; set; }
[Cassandra.Mapping.Attributes.PartitionKey]
public string Text { get; set; }
[Frozen]
public IEnumerable<LogParamsCUDT> LogParams { get; set; }
}
public class LogParamsCUDT
{
public string Key { get; set; }
public string ValueString { get; set; }
}
Note that I had to add the PartitionKey attribute or else it wouldn't run.
Here is the CQL statement that it generated:
CREATE TABLE Log (
LoggingLevel int,
UserId uuid,
TimeZone text,
Text text,
LogParams frozen<list<"testks"."logparamscudt">>,
PRIMARY KEY (Text)
)
If I remove the Frozen attribute, then this error occurs: Cassandra.InvalidQueryException: 'Non-frozen collections are not allowed inside collections: list<testks.logparamscudt>'.
If your intention is to have a column like this LogParams frozen<list<"testks"."logparamscudt">> then the Frozen attribute will work. If instead you want only the UDT to be frozen, i.e., LogParams list<frozen<"testks"."logparamscudt">>, then AFAIK the Frozen attribute won't work and you can't rely on the driver to generate the CREATE statement for you.
All my testing was done against cassandra 3.0.18 using the latest C# driver (3.10.1).

ObjectContext object error- EF

I have two tables written in EF CodeFirst:
public class DayType
{
[Key]
public int DayTypeID { get; set; }
public string NameDayType { get; set; }
public virtual ICollection<SpecialDay> Specialdays { get; set; }
public DayType() { }
}
public class SpecialDay
{
[Key]
public int SpecialDayID { get; set; }
public int Year { get; set; }
public int JanuaryDay { get; set; }
public SpecialDay() { }
public int? DayTypeId { get; set; }
public virtual DayType daytype { get; set; }
}
DBContext relation one-to-many were made by FluentAPI:
modelBuilder.Entity<DayType>().HasMany(p => p.Specialdays).WithOptional(p => p.daytype);
This code throw exception. The purpose of function is to update entity. While debugging sd had all properties. sd - is object which were selected from datagrid and then changed.
internal void Update(SpecialDay sd)
{
using (SalDBContext _db = new SalDBContext())
{
var newsd = _db.SpecialDays.FirstOrDefault(p => p.SpecialDayID==sd.SpecialDayID);
newsd.JanuaryDay = sd.JanuaryDay;
....
newsd.DecemberDay = sd.DecemberDay;
newsd.DayTypeId = sd.DayTypeId;
newsd.daytype = sd.daytype;
try
{
_db.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
At these point exception is happened _db.SaveChanges(); Exception:
{"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."} System.Exception {System.InvalidOperationException}
Would be thankful for any help with solution for my problems. Thank you
As the exception states,
"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."
Looking at the code you have there, you are assigning the daytype from the one passed in to the one you pulled from the database. Because you're creating a new SalDBContext every time you call into the Update method, you're assigning the daytype across ObjectContext objects (per the error message.
To get around that, you just need to eliminate that assignment in your Update method. Because you're assigning the FK ID in the property, you do not also need to assign the object.
One other note on EF, there is also a Find method which will go just by the ID instead of FirstOrDefault. It's a bit more optimized.
internal void Update(SpecialDay sd)
{
using (SalDBContext _db = new SalDBContext())
{
var newsd = _db.SpecialDays.Find(p => p.SpecialDayID==sd.SpecialDayID);
newsd.JanuaryDay = sd.JanuaryDay;
....
newsd.DecemberDay = sd.DecemberDay;
newsd.DayTypeId = sd.DayTypeId;
// newsd.daytype = sd.daytype; Must be eliminated!
try
{
_db.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
You have loaded sd (from method head) with anonther context as the one in your method body newsd. Therefore you received an exception on calling _db.SaveChanges();. I would say in line newsd.daytype = sd.daytype;, because it's a complex data type and represents one of your entities. Remove the line and set only the ID or load the obejct sd again for referencing.
Modified version of yours:
internal void Update(SpecialDay sd)
{
using (SalDBContext _db = new SalDBContext())
{
var newsd = _db.SpecialDays.FirstOrDefault(p => p.SpecialDayID==sd.SpecialDayID);
newsd.JanuaryDay = sd.JanuaryDay;
// ...
newsd.DecemberDay = sd.DecemberDay;
// set only the id to reference the object
newsd.DayTypeId = sd.DayTypeId;
// newsd.daytype = sd.daytype;
try
{
_db.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
Well I was some seconds to late. :)

Database items getting deleted when referencing them multiple times

I am creating a simple blogging application to get .NET MVC 4 down and I am having a problem. Everything works except for when I try to tag a blog using an array of strings for each blog like so:
public class BlogEntry
{
public List<Comment> BlogComments { get; set; }
public virtual List<String> RawTags { get; set; }
public virtual List<Tag> BlogTags { get; set; }
public virtual User Author { get; set; }
public int AuthorId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime DatePosted { get; set; }
[Key]
public int Id { get; set; }
public bool IsAcceptingComments { get; set; }
public bool IsVisible { get; set; }
public DateTime LastEdited { get; set; }
}
public class Tag
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int RefCount { get; set; }
}
Upon creating a blog and tagging it, I save tags into the BlogEntry model using this:
[HttpPost]
public int Create(string data)
{
if (data != null)
{
BlogEntry newBlog = JsonConvert.DeserializeObject<BlogEntry>(data);
newBlog.Author = Session["user"] as User;
newBlog.AuthorId = newBlog.Author.Id;
newBlog.IsVisible = true;
newBlog.IsAcceptingComments = true;
newBlog.LastEdited = DateTime.Now;
newBlog.DatePosted = DateTime.Now;
newBlog.BlogTags = new List<Tag>();
foreach (String s in newBlog.RawTags)
{
// First check to see if the tag already exists
Tag check = Db.Tags.Where(m => m.Name == s).FirstOrDefault();
if (check != null)
{
check.RefCount++;
newBlog.BlogTags.Add(check);
Db.Tags.Attach(check);
Db.Entry(check).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
}
else
{
// Create a new tag
Tag newTag = new Tag();
newTag.Name = s;
newTag.RefCount = 1;
newBlog.BlogTags.Add(newTag);
Db.Tags.Add(newTag);
}
}
Db.BlogEntries.Add(newBlog);
Db.SaveChanges();
return newBlog.Id;
}
return -1;
}
First I do a check to see if a tag already exists.. If it does, I try to add the same tag, check to the newBlog object. I would have thought that this would just save a reference to this Tag object in the DbSet, however, if I create multiple blogs posts with the tag "html" and then run a query to see what blogs have the html tag, only the most recently tagged blog retains this value.... What can I do so that I can have multiple BlogEntry objects with the same Tag object in the database?
I don't have my dev machine in front of me right now, so this is just a guess, but I figure it's better than making you wait until tomorrow...
I don't think you need the last 3 lines in your if(check!=null) and in fact, I wonder if they aren't messing you up:
Db.Tags.Attach(check);
Db.Entry(check).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
You shouldn't need to attach because you got it from the Db object already, so it should already be being tracked. This means you don't need to change the state and as for the SaveChanges, you are going to do that below.
And now another disclaimer: I've done some work with Entity Framework (version 6, if you want to know), but not with MVC, so it may be different, but my understanding is that it is better to create a new DbContext for each set of instructions, rather than having a class variable that just tracks running changes. I'm not sure if that is what you are doing or not, but it sort of looks that way from this code sample. Assuming that is relevant in MVC, you may consider creating a new DbContext (Db) at the top of your create method.
Let me know how it goes--if this doesn't help, I'll delete this answer.
First you would have to update the Tag class so that it can track its registered blog entries itself. Here the BlogEntry and Tag classes have a many-to-many relationship. So the Tag class would look like below:
public class Tag
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int RefCount { get; set; }
public virtual List<BlogEntry> BlogEntries { get; set; } // MODIFICATION
}
Now you have to add the blog entry to all of its tags for back referencing to meet your query in an easy way. Look for the modifications I have made in the for-loop below:
foreach (String s in newBlog.RawTags)
{
// First check to see if the tag already exists
Tag check = Db.Tags.Where(m => m.Name == s).FirstOrDefault();
if (check != null)
{
check.RefCount++;
check.BlogEntries.Add(newBlog); // MODIFICATION
newBlog.BlogTags.Add(check);
Db.Tags.Attach(check);
Db.Entry(check).State = System.Data.Entity.EntityState.Modified;
Db.SaveChanges();
}
else
{
// Create a new tag
Tag newTag = new Tag();
newTag.Name = s;
newTag.RefCount = 1;
newTag.BlogEntries = new List<BlogEntry>(); // MODIFICATION
newTag.BlogEntries.Add(newBlog); // MODIFICATION
newBlog.BlogTags.Add(newTag);
Db.Tags.Add(newTag);
}
}
To see what blogs have the html tag, you just have to query on the Tag class and search through the BlogEntries to get the desired blogs. Good luck!

One-To-Many Entity Framework 5 Update with code first

I have ONE entity that i want to update without updating its List of MANY entity. Im using Code-First But i cant get it to work...
Im using Ninject and everything is working except my update...
//Entities
public class A
{
public int AId { get; set; }
public string Name { get; set; }
}
public class B
{
public int BId { get; set; }
public string Name { get; set; }
public virtual List<A>ListOfAs { get; set; }
}
//Interface
private EFDbContext context = new EFDbContext();
public IQueryable<B> Bs
{
get { return context.B; }
}
public void SaveBs(B b)
{
if (b.Id== 0)
{
context.B.Add(b);
context.SaveChanges();
}
*//here i wanna call:
context.Entity(b).State = EntityState.Modified;
BUT VS dont let me... I probably missing something out...*
context.SaveChanges();
the Save method is working when i want to just add a new object to my database. But the update wont change anything...
I would appricate if anyone could tell me what im missing out...
/Thx J
Use The Following Criteria, This work for me
public BuyerInformation Update(BuyerInformation objBuyerInformation)
{
context.BuyerInformation.Attach(objBuyerInformation);
context.Entry(objBuyerInformation).State = EntityState.Modified;
context.SaveChanges();
return objBuyerInformation;
}
if (b.Id== 0)
{
context.B.Add(b);
}
else
{
context.B.Attach(b);
}
context.SaveChanges();

Categories