Używam Entity Framework w rozwiązaniu .NET 4.0 przez kilka tygodni. Jest to EF 4.3.1. Najpierw utworzyłem schemat bazy danych i wygenerowałem moje obiekty encji za pomocą szablonu "EF4.x DbContext Generator".Nie można ustawić pola/właściwości dla typu jednostki za pomocą Entity Framework 4.3.1
Miałem trzy tabele w schemacie i wszystko działało dobrze z prostymi metodami CRUD.
Dodałem czwartą tabelę "Przedmioty", która ma obce odniesienie do istniejącej tabeli "SourceUri", tak że SourceUri może mieć 0-wiele przedmiotów, a Temat ma dokładnie jedną SourceUri.
Zaktualizowałem mój model edmx i wygląda on poprawnie. Jednak bez względu na to, co staram, nie mogę wydawać się aby wykonać następujące czynności:
- Dodaj nową SourceUri rekord
- dodać jeden lub więcej przedmiotów na nowy SourceUri
Jest to kod I obecnie próbuję. Widać, że zapisuję kontekst regularnie, ale początkowo zapisywałem zmiany tylko raz na końcu metody.
/// <summary>
/// Adds a new Source URI to the system
/// </summary>
/// <param name="sourceUri">The source URI to add</param>
/// <param name="subjectNames">List of subjects for this source URI, in order</param>
/// <returns>The added source URI</returns>
public SourceUri AddSourceUri(SourceUri sourceUri, IList<string> subjectNames)
{
try
{
_logger.Debug("Adding new source URI '{0}', with '{1}' subjects.", sourceUri.Uri,
subjectNames != null ? subjectNames.Count : 0);
LogSourceUriDetails(sourceUri, "Adding");
using (var dbContext = GetDbContext())
{
dbContext.SourceUris.Add(sourceUri);
dbContext.SaveChanges(); // this save succeeds
// add the subjects if there are any
if (subjectNames != null)
{
for (int i = 0; i < subjectNames.Count; i++)
{
Subject newSubject = new Subject()
{
DisplayOrder = i,
SourceUriId = sourceUri.SourceUriId,
SubjectText = subjectNames.ElementAt(i).Trim()
};
_logger.Debug("Adding new subject '{0}' to source URI '{1}'.", newSubject.SubjectText,
sourceUri.Uri);
dbContext.Subjects.Add(newSubject); // this line fails
dbContext.SaveChanges();
}
}
_logger.Debug("Successfully added new source URI '{0}' with '{1}' subjects. Source URI ID is '{2}'.",
sourceUri.Uri, subjectNames != null ? subjectNames.Count : 0, sourceUri.SourceUriId);
return sourceUri;
}
}
catch (Exception exception)
{
_logger.ErrorException(string.Format("An error occurred adding new source URI '{0}' with '{1}' subjects.",
sourceUri.Uri, subjectNames != null ? subjectNames.Count : 0), exception);
throw;
}
}
Kod dodaje nowe urządzenie SourceUri i zapisuje zmiany. Jednak nie udało się dodać nowego obiektu do kontekstu danych. Nie stara się zapisać tej zmiany.
Wyjątkiem jest:
Unable to set field/property Subjects on entity type CommentService.DomainObjects.SourceUri. See InnerException for details.
System.Data.Objects.Internal.PocoPropertyAccessorStrategy.CollectionRemove(RelatedEnd relatedEnd, Object value)
System.Data.Objects.Internal.EntityWrapper`1.CollectionRemove(RelatedEnd relatedEnd, Object value)
System.Data.Objects.DataClasses.EntityCollection`1.RemoveFromObjectCache(IEntityWrapper wrappedEntity)
System.Data.Objects.DataClasses.RelatedEnd.Remove(IEntityWrapper wrappedEntity, Boolean doFixup, Boolean deleteEntity, Boolean deleteOwner, Boolean applyReferentialConstraints, Boolean preserveForeignKey)
System.Data.Objects.DataClasses.RelatedEnd.FixupOtherEndOfRelationshipForRemove(IEntityWrapper wrappedEntity, Boolean preserveForeignKey)
System.Data.Objects.DataClasses.RelatedEnd.Remove(IEntityWrapper wrappedEntity, Boolean doFixup, Boolean deleteEntity, Boolean deleteOwner, Boolean applyReferentialConstraints, Boolean preserveForeignKey)
System.Data.Objects.DataClasses.EntityReference`1.Exclude()
System.Data.Objects.DataClasses.RelationshipManager.RemoveRelatedEntitiesFromObjectStateManager(IEntityWrapper wrappedEntity)
System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClass5.<Add>b__4()
System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
System.Data.Entity.DbSet`1.Add(TEntity entity)
CommentService.Business.Managers.SourceUriManager.AddSourceUri(SourceUri sourceUri, IList`1 subjectNames) in C:\Projects\SVN Misc Projects\CommentService\trunk\CommentService.Business\Managers\SourceUriManager.cs:line 152
CommentService.Web.Comment.AddSourceUri(SourceUri sourceUri, IList`1 subjectNames) in C:\Projects\SVN Misc Projects\CommentService\trunk\CommentService.Web\Comment.svc.cs:line 173
SyncInvokeAddSourceUri(Object , Object[] , Object[])
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
I poszły w kółko o tym i widziałem kilka nieco inne wyjątki. Wszystkie wydają się wskazywać na fakt, że właściwość nawigacji Obiekty obiektu SourceUri jest w pewnym sensie tylko do odczytu lub o stałej długości (tablica?).
wygenerowanym klas encji wyglądać następująco:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CommentService.DomainObjects
{
using System;
using System.Collections.Generic;
public partial class SourceUri
{
public SourceUri()
{
this.Comments = new HashSet<Comment>();
this.Subjects = new HashSet<Subject>();
}
public long SourceUriId { get; set; }
public string Uri { get; set; }
public string Description { get; set; }
public System.DateTime DateCreated { get; set; }
public string AdminUser { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Subject> Subjects { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CommentService.DomainObjects
{
using System;
using System.Collections.Generic;
public partial class Subject
{
public Subject()
{
this.Comments = new HashSet<Comment>();
}
public long SubjectId { get; set; }
public long SourceUriId { get; set; }
public string SubjectText { get; set; }
public int DisplayOrder { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual SourceUri SourceUri { get; set; }
}
}
Dlaczego nie ta praca?
Szybka lista rzeczy, które sprawdzane/próbowałem:
- Schemat bazy danych wygląda poprawnie - mogę wstawić rekordy jak oczekiwano z SQL i podstawowych kluczy, kluczy obcych i tożsamości wydają się być poprawnie zachowuje
- Model wydaje się poprawnie odzwierciedlać schemat bazy danych, w tym identyfikatory PK (EntityKey = true, StoreGeneratedPattern = Identity)
- Udało mi się uzyskać EF do utrzymywania danych w moim schemacie, ale po wstawieniu danych wyjątek jest generowany stwierdzenie, że kontekst może być niezsynchronizowany - co znowu oznacza brak możliwości aby zaktualizować właściwość Nawigacja obiektów obiektu SourceUri:
- Próbowałem dodać obiekty do kolekcji dbContext.Subjects, a także do kolekcji SourceUri.Subjects. Próbowałem również ustawić właściwość SourceUri podmiotu zamiast właściwości SourceUriId podmiotu.
„* Patrz InnerException o szczegóły.” * Mówi pierwszy linia w śladzie stosu. Czy możesz to zrobić i dodać wiadomość o wyjątku wewnętrznym do swojego pytania? – Slauma
Wewnętrzny wyjątek jest pusty. –