Here's what my model looks like
public class Bios
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("init")]
public string Init { get; set; }
[BsonElement("name")]
public string Name { get; set; }
}
With MongoDB C# driver 1.10, I'm able to do the following and get the right results. I get a string list of initials.
private static IEnumerable<string> InitList(MongoCollection mongoCollection)
{
return mongoCollection.AsQueryable<Bios>().Select(b => b.ResAnInit);
}
However, I upgraded the driver to 2.0.1 and tried to get the same results. With the help of Resharper, I modified the method to this:
private static async Task<IEnumerable<string>> InitList(IMongoCollection<Bios> mongoCollection)
{
return await mongoCollection.Find(x => x.Init != null)
.Project(Builders<Bios>.Projection.Include(y => y.Init).Exclude("_id"))
.ToListAsync();
}
I get errors now. How can I fix this to do what I did with C# driver 1.10?
Related
I'm using UWP, Sqlite and Linq to make ObservableCollections to bind to my XAML Listviews. No problems thus far.
Next I like to have the results of a join query into a List, also to bind to a Listview.
This results in an error even with the simplified code below:
public class BooksAndSeries
{
public int PublYear { get; set; }
public string SeriesName { get; set; }
public string Title { get; set; }
}
public static List<BooksAndSeries> SelBooksSeries { get; set; }
public static void LoadSearchedBooks()
{
using (CatDataContext catDB = new myBooks.Model.CatDataContext())
{
var SearchedBooks = from Book bb in catDB.Books
select new BooksAndSeries { PublYear = bb.PublYear, Title = bb.Title };
SelBooksSeries = new List<BooksAndSeries>(SearchedBooks);
}
}
The query gives the error:
System.InvalidOperationException
HResult=0x80131509
Message=No coercion operator is defined between types 'myBooks.GenModule+BooksAndSeries' and 'myBooks.Model.Book'.
Source=
StackTrace:
I'm using the latest Microsoft VS Community edition and Microsoft.EntityFrameworkCore.Sqlite 1.1.5.
I'm getting this error with Breeze when I try to access the metadata url:
http://localhost:1886/api/posts/metadata
Self referencing loop detected for property 'PreviousAttribute' with type 'System.Xml.Linq.XAttribute'. Path 'root.firstAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute.nextAttribute'.
ExceptionType: "Newtonsoft.Json.JsonSerializationException"
My other URL is working and retrieving data from the database just fine
http://localhost:1886/api/Posts/posts
My post.cs has nothing that could be self referencing and looks like so
public class Post
{
public int PostID { get; set; }
public int UserID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
My controller:
[BreezeController]
public class PostsController : ApiController
{
readonly EFContextProvider<PostsDbContext> contextProvider = new EFContextProvider<PostsDbContext>();
[HttpGet]
public string Metadata()
{
return contextProvider.Metadata();
}
[HttpGet]
public IEnumerable<Post> Posts()
{
var posts = contextProvider.Context.Posts;
return posts;
}
}
I'm stuck on Windows XP using Visual Studio 2010 and EF 4 and .net 4. Are there version issues with Breeze and any of those?
The problem may be that Entity Framework generates proxy objects. Try turning these of
dbContext.Configuration.ProxyCreationEnabled = false;
Something in my project must have become corrupt, because I started over with a clean HotTowel template and the problem magically went away
If I have the following objects:
public class Application
{
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual ICollection<TestAccount> TestAccounts { get; set; }
}
public class TestAccount
{
public int TestAccountId { get; set; }
public int ApplicationId { get; set; }
public string Name { get; set; }
public virtual Application Application { get; set; }
}
EF Mapping looks like this:
modelBuilder.Entity<Application>()
.HasMany(a => a.TestAccounts)
.WithRequired(t => t.Application)
.WillCascadeOnDelete(false);
In one part of my code I want to retrieve data for Application and have
it return TestAccount data.
In another part of my code I want to retrieve data for Application and
have it NOT return TestAccount data.
Is there a way I can make this happen with LINQ or some other way?
This question has already been answered here: Disable lazy loading by default in Entity Framework 4.
Basically, in the constructor of your DbContext, just add this:
this.Configuration.LazyLoadingEnabled = false;
I hope this helps.
EDIT
Also, if you want to know how to load it manually later, it should be a simple matter of using Include() like this:
var query = context.Application.Include(x => x.TestAccounts).ToList()
How can I write a query with the build-in linq provider of NHibernate including eager loading and restrictions on the details? For example
public class Library
{
public Library()
{
Books = new List<Book>();
}
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Book> Books { get; protected internal set; }
}
public class Book
{
public Book()
{
Pages = new List<Page>();
}
public virtual int Id { get; set; }
public virtual Library Library { get; set; }
public virtual string Title { get; set; }
}
the following query shows what I need but does not load eagerly
var query = from master in session.Query<Library>()
from detail in master.Books
where detail.Title == detailValue
select master;
The following query does not work ...
var query = from master in session.Query<Library>()
// not allowed - causes Runtime error
.FetchMany(m => m.Details.Where(d => d.Value == detailValue))
select master;
Thanks a lot in advance.
Carsten
You may want to consider using queryOver here instead:-
Book book = null;
var query =
Session.QueryOver<Library>()
.Fetch(f => f.Books).Eager
.Left.JoinAlias(f => f.Books, () => book)
.Where(() => actor.book == detailValue);
I may be wrong but I don't think the NH LINQ provider can support this at the moment.
Also note the .left this is important, see this blog post for reasons why
I'm trying to get a web app working based on the S#arp Architecture. At the moment I have a the below code for my entity.
[Serializable]
public abstract class EventBase : Entity
{
#region Properties
[DomainSignature]
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual AgeRange Ages { get; set; }
public virtual int Rating { get; set; }
public virtual decimal Price { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string Website { get; set; }
public virtual EventState State { get; set; }
#endregion
protected EventBase() {}
protected EventBase(string name, string description)
{
// ReSharper disable DoNotCallOverridableMethodsInConstructor
Name = name;
Description = description;
Price = 0;
State = EventState.New;
// ReSharper restore DoNotCallOverridableMethodsInConstructor
}
}
This is mapped using Fluent NHibernate as follows
public class EventBaseMap : AutoMap<EventBase>
{
public EventBaseMap()
{
Id(x => x.ID).WithUnsavedValue(-1).GeneratedBy.Identity();
Component<AgeRange>(x => x.Ages, m =>
{
m.Map(x => x.From).TheColumnNameIs("AgeFrom");
m.Map(x => x.To).TheColumnNameIs("AgeTo");
});
JoinedSubClass<Music>("EventId", sub =>
{
sub.Map(x => x.Headliner);
});
}
}
I created a very simple repository using the very useful S#arp base repository classes.
public interface IEventRepository : INHibernateRepositoryWithTypedId<EventBase, int>
{
List<EventBase> FindByName(string searchPhase);
}
public class EventRepository : NHibernateRepository<EventBase>, IEventRepository
{
public List<EventBase> FindByName(string searchPhase)
{
return Session.Linq<EventBase>().Where(x => x.Name == searchPhase).ToList();
}
}
I can create entities in the db and return all records. When I try to test the FindByName method I get the below error.
NHibernate.QueryException: could not
resolve property: Name of:
Model.Events.EventBase
Does anyone have any ideas? Is it a problem with my mapping?
Thanks,
This is using the Auto-mapping feature. I thought you only explicitly map properties that you want to override or that don't meet the conventions?
If I add an explicit mapping this solves the issue but I am still not sure why.