Cannot add user with library version 3.3 but works with version 2.5 ?

Jan 7, 2014 at 12:16 PM
Hi everyone,

Context : VS2013, dotnet 4.5

I'm currently working on a app with the great LinqToLdap library to manage contacts and users from AD.
The code below is used into a app console in order to test the create method.
I have notice the code work well with the library version 2.5.0.0 but raise an error with the version 3.3.0.0
Actually, the error is : At least one property must be mapped. The all error log could be found at the end of this post.

So, I'm wondering if i do the right code ?
Thx in advance for your help,
string ConfigureFactoryString = "xxx.lan";
            var factory = new LdapConnectionFactory(ConfigureFactoryString);
            using (var context = new DirectoryContext(factory.GetConnection(), true))
            {
                context.Log = Console.Out;
                User user = new User
                {
                    City = "Some City",
                    CommonName = "David Vincent",
                    Country = "US",
                    FirstName = "David",
                    LastName = "Vincent",
                    Name = "David, Vincent",
                    Street = "1234 Street",
                    Title = "Mr",
                    PostalCode = "12345",
                    TelephoneNumber = "123-456-7890"
                };
                user.SetDistinguishedName();
                context.Add(user);
            }    
 public abstract class DirectoryObject
        {
            [DistinguishedName]
            public string DistinguishedName { get; set; }

            [DirectoryAttribute(StoreGenerated = true)]
            public DateTime? WhenChanged { get; set; }

            [DirectoryAttribute("cn")]
            public string CommonName { get; set; }

            [DirectoryAttribute(StoreGenerated = true)]
            public DateTime? WhenCreated { get; set; }

            [DirectoryAttribute("objectguid", StoreGenerated = true)]
            public Guid Guid { get; set; }

            [DirectoryAttribute]
            public string Name { get; set; }
        }

[DirectorySchema(NamingContext, ObjectCategory = "Person", ObjectClass = "user")]
        public class User : DirectoryObject
        {
            private const string NamingContext = "OU=Users,OU=App_Users,DC=xxx,DC=lan";

            //[DirectoryAttribute("objectsid", StoreGenerated = true)]
            public SecurityIdentifier SID { get; set; }

            [DirectoryAttribute("givenname")]
            public string FirstName { get; set; }

            [DirectoryAttribute("sn")]
            public string LastName { get; set; }

            [DirectoryAttribute]
            public string Title { get; set; }

            [DirectoryAttribute]
            public string PostalCode { get; set; }

            [DirectoryAttribute("l")]
            public string City { get; set; }

            [DirectoryAttribute("c")]
            public string Country { get; set; }

            [DirectoryAttribute]
            public string TelephoneNumber { get; set; }

            [DirectoryAttribute]
            public string Street { get; set; }

            public void SetDistinguishedName()
            {
                DistinguishedName = "CN=" + CommonName + "," + NamingContext;
            }
        }
The error log :
An error occurred while trying to add ''.
Message=At least one property must be mapped. Data=... InnerException={ } TargetSite={
} StackTrace= à LinqToLdap.Mapping.ClassMap1.Validate()
à LinqToLdap.Mapping.DirectoryMapper.<>c__DisplayClass9.<GetMapping>b__8(Type t)
à System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func`2 valueFactory)
à LinqToLdap.Mapping.DirectoryMapper.GetMapping(Type type)
à LinqToLdap.Mapping.DirectoryMapper.GetMappingT
à LinqToLdap.DirectoryContext.AddEntry[T](T entry, String distinguishedName, DirectoryControl[] c
ontrols) HelpLink=null Source=LinqToLdap HResult=-2146233088
_
Coordinator
Jan 29, 2014 at 12:12 AM
Hi cconte,

Sorry for the delayed response. A lot changed between 2.5 and 3.3. This bug has been fixed in the latest check-in. A work-around would be to use the LdapConfiugration class and add the mapping manually before use:
string ConfigureFactoryString = "xxx.lan";
var configuration = new LdapConfiguration()
     .AddMapping(new AttrbiuteClassMap<User>());

configuration.ConfigureFactory(ConfigureFactoryString);

using (var context = new DirectoryContext(configuration))
{
    context.Log = Console.Out;
    User user = new User
    {
        City = "Some City",
        CommonName = "David Vincent",
        Country = "US",
        FirstName = "David",
        LastName = "Vincent",
        Name = "David, Vincent",
        Street = "1234 Street",
        Title = "Mr",
        PostalCode = "12345",
        TelephoneNumber = "123-456-7890"
    };
    user.SetDistinguishedName();
    context.Add(user);
} 
Coordinator
Jan 29, 2014 at 12:13 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.