|
Hi,
I have 3 tables in the back end - I am trying to load them using include statement of Entity Framework TableA is parent of TableB which is parent of TableC
(from a in TableA select a).Include("TableB").Include("TableB.TableC")
Now I want to get List<tablec> using a Linq, how can I do it? I am trying in the below way - its giving me compile error - any help please? Thanks in advance.
List<TableC> cs = (from a in tablea.TableBs select a.TableCs.ToList());
Any help in achieving this, that how can I get the List of TableCs in one list using linq queries
|
|
|
|
|
If you want a list of TableC s, then start with the TableC s. Use the navigation properties to include the parent and grandparent entities if required.
List<TableC> cs = tablea.TablesCs.Include(c => c.TableB.TableA).ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes I did that - thank you for your suggestion
|
|
|
|
|
I am using Entity Framework Code First - I am getting the following message in deleting any of the tables data - like CaseNov, ViolationsNov and ViolationTypeNov are three tables which are to handle many to many relationships between Case-Nov, Violation-Nov and ViolationType-Nov, I am getting error messages even if I trying to delete the detailed tables like: CaseNov, ViolationsNov, ViolationTypeNov or row directly from NOV table, I am getting the similar type of message - any help please? I am using Entity Framework Code First for deleting it.
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.
Here is the code for deleting records any help please:
public bool Delete(NOV nov, bool performCommit = true)
{
System.Data.Entity.DbContextTransaction dbOperation = null;
if (performCommit)
dbOperation = UnitOfWork.BeginTransaction();
try
{
List<ViolationTypeNOV> novRels = UnitOfWork.ViolationTypeNOVRepository
.GetAll().Where(x => x.NOVId == nov.NOVId).ToList();
foreach (ViolationTypeNOV o in novRels)
{
nov.ViolationTypeNOVs.Remove(o);
this.UnitOfWork.ViolationTypeNOVRepository.Delete(o.ViolationTypeNOVId);
}
novRels.RemoveAll(x => x.NOVId == nov.NOVId);
List<ViolationNOV> violationNOVs = UnitOfWork.ViolationNOVRepository
.GetAll().Where(x => x.NOVId == nov.NOVId).ToList();
foreach (ViolationNOV o in violationNOVs)
{
nov.ViolationNOVs.Remove(o);
this.UnitOfWork.ViolationNOVRepository.Delete(o.ViolationNOVId);
}
violationNOVs.RemoveAll(x => x.NOVId == nov.NOVId);
List<CaseNOV> caseNOVs = UnitOfWork.CaseNOVRepository
.GetAll().Where(x => x.NOVId == nov.NOVId).ToList();
foreach (CaseNOV o in caseNOVs)
{
nov.CaseNOVs.Remove(o);
this.UnitOfWork.CaseNOVRepository.Delete(o.CaseNOVId);
}
caseNOVs.RemoveAll(x => x.NOVId == nov.NOVId);
UnitOfWork.NOVRepository.Delete(nov.NOVId);
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;
}
}
modified 6-Dec-19 12:54pm.
|
|
|
|
|
If you used "cascading deletes" you wouldn't have to worry so much about the details.
Cascade Delete - EF Core | Microsoft Docs
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I have set the cascade delete to true - here is my migration filecode for that
public override void Up()
{
DropForeignKey("dbo.Violations", "NOVId", "dbo.NOVs");
DropIndex("dbo.Violations", new[] { "NOVId" });
CreateTable(
"dbo.ViolationTypeNOVs",
c => new
{
ViolationTypeNOVId = c.Int(nullable: false, identity: true),
NOVId = c.Int(nullable: false),
ViolationTypeId = c.Int(nullable: false),
})
.PrimaryKey(t => t.ViolationTypeNOVId)
.ForeignKey("dbo.NOVs", t => t.NOVId, cascadeDelete: true)
.ForeignKey("dbo.ViolationTypes", t => t.ViolationTypeId, cascadeDelete: true)
.Index(t => new { t.NOVId, t.ViolationTypeId }, unique: true, name: "IX_UniqueConstraintViolationTypeNOV");
CreateTable(
"dbo.CaseNOVs",
c => new
{
CaseNOVId = c.Int(nullable: false, identity: true),
NOVId = c.Int(nullable: false),
CaseId = c.Int(nullable: false),
})
.PrimaryKey(t => t.CaseNOVId)
.ForeignKey("dbo.Cases", t => t.CaseId, cascadeDelete: true)
.ForeignKey("dbo.NOVs", t => t.NOVId, cascadeDelete: true)
.Index(t => new { t.NOVId, t.CaseId }, unique: true, name: "IX_UniqueConstraintCaseNov");
CreateTable(
"dbo.ViolationNOVs",
c => new
{
ViolationNOVId = c.Int(nullable: false, identity: true),
NOVId = c.Int(nullable: false),
ViolationId = c.Int(nullable: false),
})
.PrimaryKey(t => t.ViolationNOVId)
.ForeignKey("dbo.NOVs", t => t.NOVId, cascadeDelete: true)
.ForeignKey("dbo.Violations", t => t.ViolationId, cascadeDelete: true)
.Index(t => new { t.NOVId, t.ViolationId }, unique: true, name: "IX_UniqueConstraintNOVViolation");
AlterColumn("dbo.OneToManies", "ParentEntity", c => c.String());
AlterColumn("dbo.OneToManies", "ChildEntity", c => c.String());
}
Still why is it not deleting it? Any help please? One more information I am able to delete the records from SSMS though - so it must be something to do with Entity Framework Code First only though.
Unit of Work Code:
public DataAccessUnitOfWork UnitOfWork { get; set; }
public RoleManager<ApplicationRole> RoleManager { get; set; }
public UserManager<ApplicationUser> UserManager { get; set; }
public ApplicationUser CurrentUser { get; set; }
public DomainLogicManager(DataAccessUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
this.RoleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(UnitOfWork.Context));
this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(UnitOfWork.Context));
}
Delete method in one of the repository classes is as follows:
public bool Delete(IdT id)
{
T item = this.DbSet.Find(id);
var entry = this.Context.Entry(item);
entry.State = EntityState.Deleted;
this.DbSet.Attach(item);
this.DbSet.Remove(item);
this.Context.SaveChanges();
BuildMetaData(item, false, false);
return true;
}
Any help please - thank you.
modified 6-Dec-19 14:34pm.
|
|
|
|
|
Gerry Schmitz wrote: If you used "cascading deletes" you wouldn't have to worry so much about the details.
Yes I did but still I am going to through this issue - is there anyway I can find which foreign key constraint its failing at or its complaining about. Its just giving me a general statement - its not helpful for me right - if I know column name or foreign key constraint name - then it would be useful - thank you buddy.
|
|
|
|
|
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
|
|
|
|
|