|
I have a simple association Table - whose PK isn't referenced anywhere but when I am trying to delete a record from it in the following way - I am getting an Error - I am using EF Code First - any help would be very very helpful - thanks in advance.
List<ViolationTypeNOV> novRels = UnitOfWork.Context.ViolationTypeNOVs.Where(x => x.NOVId == nov.NOVId).Include("ViolationType").Include("NOV").ToList();
foreach (ViolationTypeNOV o in novRels)
{
UnitOfWork.Context.ViolationTypeNOVs.Remove(o);
}
UnitOfWork.Context.SaveChanges();
Here is the error message I am getting - if the Tables PK isn't referenced in any way - why is it failing with this error just not able to understand, I am using EF Code First - thank you.
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
The same thing if I am running through SSMS same thing is working though:
Delete ViolationTypeNOVs where ViolationTypeNOVId = 2 Why?
But again if I am running the same query through context as below, its giving me the same error at the calling of SaveChanges:
foreach (ViolationTypeNOV o in novRels)
{
string str = string.Format("Delete ViolationTypeNOVs where ViolationTypeNOVId = {0}", new object[] { o.ViolationTypeNOVId });
UnitOfWork.Context.Database.ExecuteSqlCommand(str);
}
UnitOfWork.Context.SaveChanges();
It seems like some of the objects within the context aren't nulling or getting deleted, is there anyway to clear them all in one go? Because these ids/objects are used in multiple places in the code - please let me know how to clear them all - thanks a lot.
Any help please?
modified 10-Dec-19 10:06am.
|
|
|
|
|
Fixed it - the problem is we need to clear up all the objects and its links that the parent object is using then only we can save the changes thanks here is my solution
public bool Delete(NOV nov, bool performCommit = true)
{
System.Data.Entity.DbContextTransaction dbOperation = null;
if (performCommit)
dbOperation = UnitOfWork.BeginTransaction();
try
{
List<Violation> violationIdlist = new List<Violation>();
foreach (var v in nov.ViolationNOVs)
{
var a = UnitOfWork.ViolationRepository.GetAll().Where(z => z.ViolationId == v.ViolationId).FirstOrDefault();
violationIdlist.Add(a);
}
foreach (var v in violationIdlist)
{
var a = nov.ViolationNOVs.Where(x => x.NOVId == nov.NOVId && x.ViolationId == v.ViolationId)?.FirstOrDefault();
nov.ViolationNOVs.Remove(a);
}
nov.IssuedBy.Clear();
List<ViolationTypeNOV> novRels = UnitOfWork.Context.ViolationTypeNOVs.Where(x => x.NOVId == nov.NOVId).Include("ViolationType").Include("NOV").ToList();
nov?.ViolationTypeNOVs?.Clear();
UnitOfWork.Context.ViolationTypeNOVs.RemoveRange(novRels);
List<ViolationNOV> violationNOVs = UnitOfWork.Context.ViolationNOVs.Where(x => x.NOVId == nov.NOVId).Include("Violation").Include("NOV").ToList();
nov?.ViolationNOVs?.Clear();
UnitOfWork.Context.ViolationNOVs.RemoveRange(violationNOVs);
List<CaseNOV> caseNOVs = UnitOfWork.Context.CaseNOVs.Where(x => x.NOVId == nov.NOVId).Include("Case").Include("NOV").ToList();
nov?.CaseNOVs?.Clear();
UnitOfWork.Context.CaseNOVs.RemoveRange(caseNOVs);
UnitOfWork.Context.SaveChanges();
if (dbOperation != null)
dbOperation.Commit();
LogHandler.LogInfo(2521, "Deleted NOV " + nov.NOVNumber);
return true;
}
catch (Exception ex)
{
LogHandler.LogError(2523, "Commit Fail in NOV Delete", ex);
if (dbOperation != null)
dbOperation.Rollback();
throw ex;
}
}
This statement has fixed the problem: UnitOfWork.Context.ViolationTypeNOVs.RemoveRange(novRels); thanks a lot for everybody who tried to help me
|
|
|
|
|
I have a Linq statement as below
{ if (!inspectionItemIds.Contains((int)item.InspectionItemId)) inspectionItemIds.Add((int)item.InspectionItemId); });
And item.InspectionItemId is coming as null and its throwing me exception as below:
System.InvalidOperationException
HResult=0x80131509
Message=Nullable object must have a value.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Nullable`1.get_Value()
at IMS.Model.DomainLogic.ViolationManager.<>c__DisplayClass19_0.<ViolationsFor>b__9(Violation item) in C:\SourceCode\IMS\Development\IMS\IMS.Model\DomainLogic\ViolationManager.cs:line 254
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at IMS.Model.DomainLogic.ViolationManager.ViolationsFor(String for, Int32 id) in C:\SourceCode\IMS\Development\IMS\IMS.Model\DomainLogic\ViolationManager.cs:line 253
at IMS.Web.Controllers.CaseController.Details(Int32 id, Boolean isPrint) in C:\SourceCode\IMS\Development\IMS\IMS.Web\Controllers\CaseController.cs:line 382
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
Any help to resolve these types of issues? Any help would be very very helpful - thanks in advance.
|
|
|
|
|
There is no Linq in the code you showed.
At a wild guess, you could try
if (item.InspectionItemId.HasValue && !inspectionItemIds.Contains(item.InspectionItemId.Value))
{
inspectionItemIds.Add(item.InspectionItemId.Value);
}
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
At a guess, and assuming that item.InspectionItemId is the nullable type, then it does not contain a value. Using your debugger will quickly determine if that is the case.
|
|
|
|
|
Another option, if you're using C# 7:
if (item.InspectionItemId is int inspectionItemId && !inspectionItemIds.Contains(inspectionItemId))
{
inspectionItemIds.Add(inspectionItemId);
} Or, if inspectionItemIds is a HashSet<int> , or some other collection which only stores unique items:
if (item.InspectionItemId is int inspectionItemId)
{
inspectionItemIds.Add(inspectionItemId);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks to all of you guys - I resolved it and your suggestions also helped me, I was just out of my mind - I shouldn't have put this question - just too much of stress as some release is happening.
Thanks for understanding
|
|
|
|
|
It really helps other people if, when you resolve something, you actually show your solution.
|
|
|
|
|
Hi guys, I’m making a Windows Forms application for a company. It needs database management and that kind of things, so I decided that I was going to use Entity Framework based on an SQL database.
The thing is, that I don’t know how could it be possible to connect multiple computers to the same database. It is such a small company, that they don’t have internet, so I don’t know how could I achieve this. I thought about having the database file saved in computer A, and computer B and C should connect to it, even through usb cables. I don’t know if this is the best practice, and if it is, how could I do it?
|
|
|
|
|
You install SQL Server on one computer, and create the database there.
All other computers that need to use your database will need to be able to connect to the SQL Server computer, so you'll need to enable remote connections, and possibly add an exception to the Windows firewall.
How to configure SQL Server 2005 to allow remote connections[^]
You configure your application's connection string to point to the single SQL Server instance, and the computers will all be using the same database.
SQL Server connection strings - ConnectionStrings.com[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Make sure that the SQL server on a machine is accessible from all computers you intent the app to work. You also may have to open ports used by the SQL server in the firewall. You can then update the connection string to connect to the SQL instance.
|
|
|
|
|
First of all you have to change localhost with the host name or with the ip of the sql server hosting pc. then you have to enable tcp/ip on the sql server Configuration Manager on the hosting pc then go to SQL Server Network Configuration and click on Protocols for SQLEXPRESS. Then you will see that TCP/IP is disabled. Double Click on TCP/IP and select yes to enabled.
|
|
|
|
|
Quote: I thought about having the database file saved in computer A, and computer B and C should connect to it, even through usb cables. I don’t know if this is the best practice, and if it is, how could I do it?
Sorry, you're wrong.
You need only one database installed on a single computer (let it be computer A). You have to install an SQL server on it and enable remote connections to that SQL server instance. Other computers (B-Z) have to have intalled an application, which connection string refers to SQL server instance on computer A. I'd suggest to get connection string from config file. See: Connection Strings and Configuration Files | Microsoft Docs
For further details, please see Richard's MacCutchan Deeming answer.
[EDIT]
Sorry, Richard.
modified 3-Dec-19 9:21am.
|
|
|
|
|
Maciej Los wrote: For further details, please see Richard's MacCutchan answer.
Really? I know I had a birthday recently, but RMC's still got a few years on me.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Oooops!
I owe you a box of good polish
|
|
|
|
|
Maciej Los wrote: good polish
Any recommendations beyond Zywiec Porter? It's been a few years since my last visit.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sounds like you are a fan of dark beer...
Yes, there's few other brands, like: Okocim, Łomża and few less known companies (small, local breweries).
Cheers!
Maciej
modified 3-Dec-19 9:45am.
|
|
|
|
|
Thanks - I'll keep an eye out for them. Although I might avoid the piwo bezalkoholowe version.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you, apology accepted. But it was nice being an SQL expert for a while.
|
|
|
|
|
Nobody has mentioned it yet, but networking through USB cables isn't going to work.
Use Ethernet or your network doesn't happen in a supportable way.
|
|
|
|
|
|
When I use the connection string ‘\\\\network\share\file.db’ it works fine. But if the share is hidden the SQLite database can not be found.
I use the connectionstring as follows:
Con As New SQLite.SQLiteConnection With {.ConnectionString = "Data Source=\\\\network\share\file.db;Version=3;"}
The \\\\ is according this article: unable to open database file on a local network
|
|
|
|
|
If the user running the code doesn't have read and write permissions to share and to the folder the database is in, it's not going to open.
There is no such thing as a "hidden"(*) share. You either have permissions to use it or you don't.
* There are admin shares, where the name ends in a $, but you need admin permissions to see them. Normal users will never know they exist.
|
|
|
|
|
@"\\network\share\file.db"
or
"\\\\network\\share\\file.db"
..but what Mish answered, won't work
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I need a efficient , verbose and efficient analytical tool which I want to integrate in my application. The requirement or expectations are -
1. Online feedback.
2. Provide a more in-depth version of the quick start guide, with section headings acting as individual articles.
3. Product Tour for New Users.
Also to add my application is designed purely in .NET framework using MVC (may integrate to .NET core in future). So suggest something which is compatible to the technology stack as well.
Thanks
|
|
|
|