我在通过Linq获取“SaleToBarbari”类型列表到实体并使用批量插入将它们插入数据库时遇到了一些问题。 我一直收到错误“序列包含多个元素”。有任何想法吗? 这是我的代码:
var bas = new SaleToFreightageEntities();
bas.Configuration.AutoDetectChangesEnabled = false;
bas.Configuration.ValidateOnSaveEnabled = false;
var L = (from a in bas.Last
select a).FirstOrDefault();
if (L != null)
{
Last = Convert.ToInt64(L.LastID);
}
var w = (from l in bas.RadifsSendCenter
where l.id_rec > Last
select new { l.id_rec }).ToList();
if (w.Count>0 && (w.Last().id_rec > Last))
{
bas.Last.Update(t => new Last { BeforLast = L.LastID });
var q = (from d in bas.RadifsSendCenter
where d.id_rec > Last
select new
{
d.id_rec,
d.Radifkolsal,
d.NameKala,
d.ShenaseKala,
d.Vazn,
d.Bandal,
d.NameGreid,
d.Dobaskul,
d.TedadBas,
d.del,
d.Tozih,
d.NoeShemsh,
d.Metrazh,
d.Keyfiat,
d.Address,
d.City,
d.Tel,
d.ShenaseMeli,
d.Shenase,
d.Tolid,
d.Rahgiry,
d.Sefaresh,
d.Karbar,
d.TimeErsal,
d.DateErsal,
d.CodePosti
}).ToList();
var u = (from bu in bas.OperatorTable
where bu.OperatorName == LoginForm.Username && bu.Type
select bu).FirstOrDefault();
var list = q.Select(b => new SaleToBarbari
{
SaleID = b.id_rec,
Radifkolsal = TrimEnd(b.Radifkolsal),
CodeKala = TrimEnd(b.ShenaseKala),
NameKala = TrimEnd(b.NameKala),
Bandal = b.Bandal,
TedadBaskool = b.TedadBas,
Vazn = b.Vazn,
del = b.del,
Metrazh = b.Metrazh,
City = TrimEnd(b.City),
ShenaseMeli = TrimEnd(b.ShenaseMeli),
Greid = TrimEnd(b.NameGreid),
TolidCondition = TrimEnd(b.Tolid),
Shenase = TrimEnd(b.Shenase),
Keyfiat = b.Keyfiat,
Tell = TrimEnd(b.Tel),
Address = TrimEnd(b.Address),
SaleTozihat = TrimEnd(b.Tozih),
NoeShemsh = TrimEnd(b.NoeShemsh),
UserReceivedID = u.ID,
Rahgiry = TrimEnd(b.Rahgiry),
Sefaresh = TrimEnd(b.Sefaresh),
CodePosti = TrimEnd(b.CodePosti),
SaleOperator = TrimEnd(b.Karbar)
}).ToList();
var options = new BulkInsertOptions
{
EnableStreaming = true
};
bas.BulkInsert(list, options);
我搜索了很多但没有任何帮助。 这是我的Stack-trace:
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at EntityFramework.MappingAPI.Mappers.DbFirstMapper.GetTableName(String typeFullName) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\DbFirstMapper.cs:line 88
at EntityFramework.MappingAPI.Mappers.DbFirstMapper.PrepareMapping(String typeFullName, EdmType edmItem) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\DbFirstMapper.cs:line 26
at EntityFramework.MappingAPI.Mappers.MapperBase.MapEntity(String typeFullName, EdmType edmItem) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\MapperBase.cs:line 330
at EntityFramework.MappingAPI.Mappings.DbMapping..ctor(DbContext context) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappings\DbMapping.cs:line 82
at EntityFramework.MappingAPI.EfMap.Get(DbContext context) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\EfMap.cs:line 60
at EntityFramework.MappingAPI.Extensions.MappingApiExtensions.Db(DbContext ctx, Type type) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Extensions\MappingApiExtensions.cs:line 51
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at EntityFramework.BulkInsert.Helpers.MappedDataReader`1..ctor(IEnumerable`1 enumerable, IEfBulkInsertProvider provider) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Helpers\MappedDataReader.cs:line 58
at EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable`1 entities, SqlTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:line 22
at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities, IDbTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 77
at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 96
答案 0 :(得分:0)
最后我找到了问题并修复了它。 问题是在我的视图表中添加到模型与映射错误相同的字段,通过修复它们的问题消失了。 也许这对某人有帮助。